bw2801
bw2801

Reputation: 317

PDO - 3D000 No database selected

I wanted to create a PDO mysql connection. But the execute() function returns false and the errorInfo() returns "No database selected!". But I selected a database.

This is my code:

    $array = array("db" => "blogscript", "host" => "localhost", "user" => "root", "pass" => "");

    $db = new PDO('mysql:dbname=' . $array['db'] . ';host=' . $array['host'] . '', $array['user'], $array['pass']);

    $statement = $db->prepare('
        SELECT *
          FROM pages
    ');

    $r = $statement->execute();
    if ($r === false) {
        return $statement->errorInfo();
    }

The database "blogscript" existst.

Upvotes: 0

Views: 7556

Answers (1)

david strachan
david strachan

Reputation: 7228

Hard coding the connection with database & host in this order

$db = new PDO('dbname=blogscrip;mysql:host=localhost', root, pass); 

Throws exception could not find driver

in the order in documentation

$dbh = new PDO('mysql:host=localhost;dbname=blogscript', root, pass);

Works

Change the order to host & database

Upvotes: 6

Related Questions