-2

Just had the database.php file changed from from MySQL to PDO. But I'm getting this error:

Fatal error: Call to a member function query() on a non-object in /home/mypath/mydomain.com/library/database.php on line 19

It's working fine on the site of the individual who made the change. But when uploaded to my site it's generating this error, not sure why. I use DreamHost. What's wrong?

Line 19:

$result = $pdo->query($sql);    

Code prior:

<?php
require_once 'config.php';

try {
  $conn = new PDO("mysql:host=$servername;dbname=onlysecret_db", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
  echo "Connection failed: " . $e->getMessage();
}

function dbQuery($sql)
{
  global $pdo;
  $result = $pdo->query($sql);  
  return $result;
}
fragilewindows
  • 1,394
  • 1
  • 15
  • 26
reallyreally
  • 31
  • 1
  • 4

1 Answers1

0

It's because you're accessing $pdo, which is a variable that doesn't exist.

Change $pdo to $conn:

function dbQuery($sql)
{
    global $conn;
    $result = $conn->query($sql);    
    return $result;
}

...but I would advise against using global and instead pass the $conn as a parameter to the function.

Ben
  • 8,894
  • 7
  • 44
  • 80