0

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.

bw2801
  • 321
  • 2
  • 8
  • 15

1 Answers1

6

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

david strachan
  • 7,174
  • 2
  • 23
  • 33
  • I made it like you said but there is the same error: `3D000 - 1046 - No database selected!`. – bw2801 Jan 11 '13 at 14:48
  • 1
    Glad to help.I find if you try to do things by the manual first before you try to experimentand and then add things incrementally.This way you know where the errors are occuring – david strachan Jan 11 '13 at 15:28