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 />"; }
source share