Using PDO to Connect Mysql Database Doesn't Work

I am trying to use the PDO module in my PHP code to connect to a database. I read and searched for similar topics, but I can’t understand what I did wrong. Please help me solve the problem.

  • Apache Version: Apache / 2.2.21 (Win32) PHP / 5.3.10

  • in the php.ini file, I commented out the line: extension = php_mysql.dll

    2a. The phpinfo function showed that the location of the loaded configuration file is C: \ php \ php.ini

    2b. PDO driver information displayed by phpinfo function: in the PDO section: PDO drivers β†’ Mysql (included) in the PDO section for Driver for MySQL: client API version β†’ mysqlnd 5.0.8-dev - 20102224 - $ Edition: 321634 $ (included)

    The code I used to connect to the database

    
    
    

    $db_user = "uid"; $db_pass = "pd";

    $db_connect = new PDO('mysql:host=locahost; dbname=practice; charset=UTF-8', $db_user, $db_pass); if($db_connect){ print "connected to the db " . "
    "; }

    else{ print "error connects to the db. " . mysql_error(); }

    ?>

    The error message I received:
  • Warning: PDO :: __ construct () [pdo .-- construct]: php_network_getaddresses: getaddrinfo failed: this host is not known. in C: \ server \ htdocs \ html-exer \ handle_reg3.php on line 14
  • Warning: PDO :: __ construct () [pdo .-- construct]: [2002] php_network_getaddresses: getaddrinfo failed: such a host is not known. (connection attempt via tcp: // locahost: 3306) in C: \ server \ htdocs \ html-exer \ handle_reg3.php on line 14
  • Fatal error: thrown a "PDOException" exception with the message "SQLSTATE [HY000] [2002] php_network_getaddresses: getaddrinfo failed: this host is not known. 'In C: \ server \ htdocs \ html-exer \ handle_reg3.php: 14 Stack trace: # 0 C: \ server \ htdocs \ html-exer \ handle_reg3.php (14): PDO β†’ __ construct ('mysql: host = loca ...', 'root', 'password') # 1 {main} selected in C: \ server \ htdocs \ html-exer \ handle_reg3.php on line 14

Edit: An answer has been added asking for additional information, which will undoubtedly be deleted in the near future:

Hi, your common sense: Thanks for the code snippet. It helped me solve this problem. It seems that the encoding may be the cause. Here is my code for connecting to db



 $dsn= 'mysql:host=localhost; dbname=practice; charset=utf8'; $db_user = "root"; $db_pass = "mypd"; $db_connect = new PDO($dsn, $db_user, $db_pass); if($db_connect){ print "connected to the db " . "<br />"; } 

+6
source share
4 answers

It seems your server is just misconfigured
use 127.0.0.1 instead of localhost in the DSN.

 $dsn = 'mysql:host=127.0.0.1; dbname=practice; charset=utf8'; $pdo = new PDO($dsn, $db_user, $db_pass, $opt); 

You need to read error messages.
He says that your PDO has problems connecting to localhost. So, you need to change the address for the PDO connection string.

Also you used the wrong encoding name, I fixed it

In addition, mysql_error() useless with PDO. No need to call this function, you already have an error

+7
source

Just in case, if someone else comes here from Google. I had the same problem and restarting apache fixed it.

+3
source

If all of the above answers do not solve the problem, try specifying the port number for your DSN:

 $this->con = new PDO("mysql:host=localhost;port=3306;dbname=my_db","root","secret"); 

I used 3306 here, as this is my default port number for Mysql.

+2
source

The first thing to do is double check the connection settings in the configuration file

This may seem obvious, but since I just configured the .yml options of my Symfony project with the β€œcorrect values,” I did not immediately think about it and thought that the problem was somewhere else.
My dbname was just wrong!

0
source

All Articles