Error when connect to Database through PDO

I get the following error when I try to connect to the Database through PDO:

Warning: PDO::__construct() expects parameter 4 to be array, string given in ****/search.php on line 98

Open in new window


Line 98:
    $pdo = new PDO($dsn, $db_user, $db_word, $db_port);

Open in new window


All script for connection:

error_reporting(E_ALL);

$db_host = "****";
$db_user = "*****"; 
$db_word = "*****"; 
$db_name =  "*****";
$db_port = "****";

$dsn = "mysql:host=$db_host; dbname=$db_name";
try
{
    $pdo = new PDO($dsn, $db_user, $db_word, $db_port);
}
catch(PDOException $exc)
{
    var_dump($exc);
    trigger_error('NO PDO Connection', E_USER_ERROR);
}

Open in new window

Klaus AndersenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
The fourth parameter of PDO should be an array. See the constructor:

public __construct ( string $dsn [, string $username [, string $password [, array $driver_options ]]] )

Open in new window

The port should be a part of $dsn.

HTH,
Dan
0
Klaus AndersenAuthor Commented:
Thanks @DanCraciun,

I'm just starting to know general concepts about PDO.

What do you mean about $driver_options?
Should the $db_port go as an array?
0
Beverley PortlockCommented:
Drop the fourth parameter like so

error_reporting(E_ALL);

$db_host = "****";
$db_user = "*****";
$db_word = "*****";
$db_name =  "*****";
$db_port = "****";

$dsn = "mysql:host=$db_host; dbname=$db_name; port=$db_port";
try
{
    $pdo = new PDO($dsn, $db_user, $db_word );
}
catch(PDOException $exc)
{
    var_dump($exc);
    trigger_error('NO PDO Connection', E_USER_ERROR);
}
                                  

Open in new window


Unfortunately I do not have PDO installed so I cannot test this.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Klaus AndersenAuthor Commented:
Thanks @bportlock! It works like charm!
0
Beverley PortlockCommented:
I am glad it worked OK. Thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.