Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6319873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:52:38+00:00 2026-05-24T15:52:38+00:00

<?php # Nettuts Tutorial using PHP Data Objects (PDO), /**This file contains the database

  • 0
<?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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:52:39+00:00Added an answer on May 24, 2026 at 3:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP stores its session information on the file system of the host of the
PHP Version: 5.3.3 I'm using the SOAP extension to pull data from a web
PHP, for all its warts, is pretty good on this count. There's no difference
php newbie here..I need some PHP help ideas/examples on how to import data from
PHP is a largely flexible language, I could do this anyhow and anywhere. However,
PHP's range function work like this in php : $leap_years = range(1900, 2000, 4);
PHP supports this: $z = 5; $str = z is $z; // result: z
PHP, 5.3 non thread safe window, also proven on PHP 5.2.9 on linux. Sample
PHP has a great function called htmlspecialcharacters() where you pass it a string and
PHP's explode function returns an array of strings split on some provided substring. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.