Reputation: 49
So I've had this code running for PDO connection to my database. Since last couple of hours, I'm getting a weird error called "invalid data source name".
I've searched quite a bit but I'm getting no solutions for it. What might be the reason?
Connection Code
<?php
$connectionString = 'mysqlhost=127.0.0.1;dbname=cdm';
try
{
$conn = new PDO($connectionString, 'root', 'PASS1234');
$conn->setAttribute(PDOATTR_ERRMODE, PDOERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
var_dump($conn);
?>
Output
invalid data source nameNULL
Upvotes: 2
Views: 10066
Reputation: 4772
Your entire code is totally wrong.
first fix :
$connectionString = 'mysqlhost=127.0.0.1;dbname=cdm';
to $connectionString = 'mysql:host=127.0.0.1;dbname=dgsa';
Then change the following :
$conn->setAttribute(PDOATTR_ERRMODE, PDOERRMODE_EXCEPTION);
To :
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Full correct code :
<?php
$connectionString = 'mysql:host=127.0.0.1;dbname=dgsa';
try
{
$conn = new PDO($connectionString, 'root', 'PASS1234');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
var_dump($conn);
?>
Upvotes: 6
Reputation: 551
I think you need to change the variable to this :
$connectionString = 'mysql:host=127.0.0.1;dbname=cdm';
Upvotes: 3
Reputation: 2975
I think it's just a typo
$connectionString = 'mysqlhost=127.0.0.1;dbname=cdm';
should be
$connectionString = 'mysql:host=127.0.0.1;dbname=cdm';
( with : )
Upvotes: 0