9
<?php # Nettuts Tutorial using PHP Data Objects (PDO),

/**This file contains the database access information
 *This file also establishes a connection to mySQL
 *and selects the database.
 *Set the database access information as constants:
 **/
// print_r(PDO::getAvailableDrivers());

DEFINE('DB_USER', 'root');
DEFINE('DB_PASSWORD', 'root');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_NAME', 'sitename');

$php = "htmlspecialchars";
try {
    #MySQL with PDO_MYSQL
    // $DBH = new PDO("mysql:host={$php(DB_HOST)}; dbname={$php(DB_NAME)}", root, root};  
    $DBH = new PDO("mysql:host=localhost; dbname= sitename", root, root);

    $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    # UH-OH! Typed DELECT instead of SELECT!
    $DBH->prepare('DELECT name FROM people');
} catch (PDOException $e) {
    echo "I'm sorry, Dave. I'm afraid I can't do that.";
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
?>

The Console in OS X returns "[14-Aug-2011 15:59:59] PHP Notice: Use of undefined constant root - assumed 'root' in /Applications/MAMP/htdocs3/nettuts/PHP/PDO for Database Access/mysql_pdo_connect.php on line 20."

I've "googled" and found a partial answer here. So I was hoping for completion here.

TIA

Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55
Wasabi
  • 1,591
  • 1
  • 16
  • 22
  • 1
    You should also not use the `$php = "htmlspecialchars"` workaround to interpolate constants. That's possible, but not one of the special cases where this is useful. Rather break up you PDO DSN init string and concat the constants normally: `= new PDO("mysql:host=".DB_HOST." dbname=".DB_NAME.";")` – mario Aug 14 '11 at 23:13

3 Answers3

21

You do:

$DBH = new PDO("mysql:host=localhost; dbname= sitename", root, root); 

Which should have been:

$DBH = new PDO("mysql:host=localhost; dbname= sitename", 'root', 'root'); 

With quotes. Otherwise PHP thinks it is some constant instead of a string. However by looking at your code I see you have defined constants to access the database so why don't you simply do:

$DBH = new PDO('mysql:host='.DB_HOST.'; dbname='.DB_NAME, DB_USER, DB_PASSWORD); 

UPDATE

I also see that you are using MySQL with PDO. Please note that in order to safely use MySQL with PDO you have to disable emulated prepared statements:

$DBH = new PDO('mysql:host='.DB_HOST.'; dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASSWORD);
$DBH->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

Please note that I have also set the encoding in the DSN string (in my case to utf8). I also wonder if you really need the constants in your code, because often you will only need one connection per request (to the same database) so you don't need to have those globals floating around if you just create the connection once and pass the connection to the parts of the code that need it. See also my somewhat related question for more information about this.

Community
  • 1
  • 1
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
4

You haven't defined a constant called root anywhere in the above code. Instead, you've defined a constant called DB_USER with the value of "root", and another called DB_PASSWORD with the same value. Take a look at the PHP documentation page for define() for more information on how this type of constant works.

Try this:

$DBH = new PDO( "mysql:host=" . DB_HOST . "; dbname=" . DB_NAME, 
                DB_USER, 
                DB_PASSWORD
              ); 
AgentConundrum
  • 20,288
  • 6
  • 64
  • 99
  • The arrangement of those quotes is just what I needed. I had ...new PDO("mysql:host=DB_SERVER..."), which was causing an error for me. – Mark Oct 07 '16 at 18:25
0

You need to put your 'root' string in a single quotes

zerkms
  • 249,484
  • 69
  • 436
  • 539