user2061466
user2061466

Reputation: 485

using PDO to connect Mysql database not working

I am trying to using the PDO module in my PHP code to connect to the database. I have read and search the similar topics, but I can't figure out what I have done wrong. Please help me to solve the issue.

  1. Apache version: Apache/2.2.21 (Win32) PHP/5.3.10

  2. in the php.ini file, I un-commented the line: extension=php_mysql.dll

    2a. phpinfo function showed 'Loaded Configuration File' location is C:\php\php.ini

    2b. PDO driver information showed by phpinfo function: under PDO section: PDO drivers-->Mysql (enabled) under PDO Driver for MySQL section: client API version-->mysqlnd 5.0.8-dev - 20102224 - $Revision: 321634 $ (enabled)

Code I used to connection 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 " . "<br />";
} else{
    print "error connects to the db. " . mysql_error();
}

The error message I received:

Edit: Added answer asking for further information that will no doubt be deleted shortly:

Hello Your Common Sense: Thanks for the code fragment. It helped me to resolve the issue. It appears that the charset may be the cause. Here is my code to connect to the 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 />";
 }

Upvotes: 8

Views: 30296

Answers (4)

Balmipour
Balmipour

Reputation: 3055

The first thing to do is to double check your connection parameters in your config file

This might seem obvious, but since I had just configured the parameters.yml of my Symfony project with "the right values", I didn't think about it immediately, and thought the issue was somewhere else.
My dbname was just wrong !

Upvotes: 0

Gokigooooks
Gokigooooks

Reputation: 794

If all the above answers does not solve your problem try specifying the portnumber for your DSN :

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

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

Upvotes: 2

Tom B
Tom B

Reputation: 2923

Just in case anyone else lands here from google. I had the exact same problem and restarting apache fixed it.

Upvotes: 3

Your Common Sense
Your Common Sense

Reputation: 157839

It seems your server just misconfigured
use 127.0.0.1 instead of localhost in 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.
It says your PDO have problems connecting to localhost. So, you need to change an address for PDO connect string.

Also you were using wrong charset name, I've corrected it

Also, mysql_error() is useless with PDO. No need to call this function, you have an error thrown already

Upvotes: 10

Related Questions