2
<?php
#require_once('inc/dbc1.php');
$dsn = 'mysql:dbname=dbname;host=somehost;
$user = 'someuser';
$password = 'SomePass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

$pdo1 = new PDO($dsn, $user, $password);
$pdo1->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth1 = $pdo1->prepare('SELECT pID, lname, fname FROM Professor ORDER BY pID DESC LIMIT 5;');
$sth1->execute(array());
?>

Throws the error:

Uncaught exception 'PDOException' with message 'invalid data source name' in  PDO->__construct('', NULL, NULL) on line 1

Anyone see anything wrong with this?

1
  • In my opinion, this issue must be caused by your setting string, please double check your settings. Perhaps caused by your dsn string, how about add the port 3306 by default.
    – Ceven
    Aug 17, 2013 at 4:15

1 Answer 1

9

you have

$dsn = 'mysql:dbname=dbname;host=somehost;

maybe just maybe ...

$dsn = 'mysql:dbname=dbname;host=somehost';

unless this was a mouso when cut-and-pasting the question.

1
  • 2
    good spotted! But really this should be spotted by the IDE used by JohnPaul. Aug 17, 2013 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.