PDO does not connect to Mysql

Asked

Viewed 3,667 times

3

I’m trying to make a connection to the bank by PDO, but every time I try to make this connection, the message appears:

could not find driver

I went back to find out and learned that I had to enable PDO in php.ini. OK, I went there and took the ; from where I needed, but still the message 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?

  • You restarted PHP after changing the php.ini? What operating system is using?

  • @user2154508 Which version of php you are using? and which line was uncommented?

  • Which operating system you are using?

  • I am using Windows, and the version of my PHP is: PHP Version 5.2.6

  • Dude, are you sure you’re using PHP 5.2.6? If so, do you have a special reason? Otherwise, update this. It’s not the direct answer to your problem, but this version of is archaic.

  • Hello, can you tell what is the error returned ?

  • @Edilson is at the beginning of the question: could not find driver

  • @user2154508 beyond the pdo_mysql line you set up the php extensions folder in the file . ini ?

Show 3 more comments

7 answers

2

The extent of mysql is not installed or you do not have the mysql installed in the localhost.

Create a file info.php at the root of the webserver, with function phpinfo(); and then access localhost/info.php and look for pdo, There you see if the driver pdo for mysql is installed.

2

On 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, on the terminal type:

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

//reiniciando o apache
sudo service apache2 restart

2

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

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

1

A quick and easy way to test if mysql is available is by own PDO with its function PDO::getAvailableDrivers()

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

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

1

When this kind of problem occurs what can happen:

  • if you are using windows. may not have the respective dll
  • Mysql port installed is not the default (3306)
  • Database data is not correct

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;
}

0

Execute php -m in the terminal/prompt and check that the pdo_mysql extension is actually enabled, if it is and still doesn’t work check that Mysql has been started.

  • 4

    That should be a comment, not an answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.