PDO does not connect to MySQL

3

I'm trying to make a connection to the database via PDO, but every time I try to make that connection, the following message appears:

could not find driver

I went back to know and I knew that I had to enable the PDO in php.ini. Okay, I went there and took the; from where I needed it, but still the message still appears.

My code is this:

<?php

try{
// Faz conexão com banco de daddos
$pdo = new PDO("
    mysql:host=localhost;
    dbname=servidores;", 
    "root", 
    "root");
}catch(PDOException $e){
// Caso ocorra algum erro na conexão com o banco, exibe a mensagem
echo 'Falha ao conectar no banco de dados: '.$e->getMessage();
die;
}

?>

Does anyone know what it can be?

    
asked by anonymous 09.02.2014 / 19:29

7 answers

2

The mysql extension is not installed or you do not have mysql installed in localhost .

Create a info.php file in the root of the webserver, with the function phpinfo(); and then go to localhost/info.php and look for pdo , see if the pdo driver for mysql is installed. / p>     

09.02.2014 / 21:21
2

In linux, type in the terminal:

php -i | grep drivers

And make sure the driver is enabled. On Debian / Ubuntu based systems the installation is simple, in the terminal type:

//para instalar
sudo apt-get install php5-mysql

//reiniciando o apache
sudo service apache2 restart
    
10.02.2014 / 01:56
2

It is quite common to occur, especially with people developing in the Windows environment in versions prior to 5.3, where there is no need to enable a separate extension for the PDO itself to enable only the DLL of the PDO (php_pdo.dll) and forget to enable the DLL (s) of the DBMS (s) supported by the PDO that they will be using.

The solution, also simple, is to edit the PHP.INI by removing the comment signal (;) from the line referring to, in this case, php_pdo_mysql.dll

    
11.02.2014 / 20:30
1

When this type of problem occurs, what may happen:

  • if you are using windows. may not have the respective dll
  • The installed MySql port is not the default (3306)
  • Database data is not correct
10.02.2014 / 00:52
1

A quick and easy way to test if mySQL is available is by the PDO itself with its PDO: : getAvailableDrivers ()

  $drivers = PDO::getAvailableDrivers();
    foreach ($drivers as $nome) {
      echo 'Disponivel: ' . $nome . '<br />';
    }

This way you list all the drivers the PDO can use.

    
18.09.2014 / 22:31
0

Run php -m on the terminal / prompt and check that the pdo_mysql extension is actually enabled, if it is and still does not work make sure MySQL is started.

    
09.02.2014 / 20:18
0

That's how it works.

try{
    // Faz conexão com banco de daddos
    $host = ('localhost');
    $user = ('root');
    $pass = ('root');
    $bancodb = ('curso_php');

    $conecta = new PDO("mysql:host=$host;dbname=$bancodb", $user, $pass);
}catch(PDOException $e){
    // Caso ocorra algum erro na conexão com o banco, exibe a mensagem
    echo 'Falha ao conectar no banco de dados: '.$e->getMessage();
    die;
}
    
25.02.2015 / 02:45