0

I am getting following fatal error when i use PDO to connect and retrieve some output from MySQL

Fatal error: Call to a member function prepare() on a non-object in /home/ ... line 21

My PHP Code:

$dbConnection = new PDO('mysql:dbname=abc;host=127.0.0.1;charset=utf8', 'abc','abc');
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('SELECT current_date()'); //line 21
$stmt->execute();
5
  • 4
    You don't read "your" code (copied from somewhere?) so you've got errors. Commented Oct 9, 2013 at 19:01
  • yes... i thought "new PDO" has become $pdo :( Commented Oct 9, 2013 at 19:04
  • @ElonThan : could you please let me know how to read the row values then ? is it $row[0] ? Commented Oct 9, 2013 at 19:15
  • No, I can't. Go and learn PHP before trying to write code in it. Commented Oct 9, 2013 at 19:17
  • Thats expected !! but I could not find enough detail on php.net/manual/en/ref.pdo-mysql.php... Commented Oct 9, 2013 at 19:22

2 Answers 2

3

change this line:

$stmt = $pdo->prepare('SELECT current_date()'); //line 21

To:

$stmt = $dbConnection->prepare('SELECT current_date()'); //line 21

Because $pdo is not defined here but $dbConnection

Sign up to request clarification or add additional context in comments.

1 Comment

could you please let me know how to read the row values then ? is it $row[0] ?
1

try changing the 4th line to

$stmt= $dbConnection->prepare...

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.