开发者

Call to undefined function query() - using PDO in PHP

I have a written a script in PHP that reads from data from a MySQL database. I am new to PHP and I have been using the PDO library. I have been developing on a Windows machine using the WAMP Server 2 and all has been working well. However, when I uploaded my script to the LINUX server where it will be used I get the following error when I run my script.

Fatal error: Call to undefined function query()

This is the line where the error is occuring ...

foreach($dbconn->query($sql) as $row)

The variable $dbconn is first defined in my dblogin.php include file which I am listing below.

<?php 
// Login info for the database
$db_hostname = 'localhost';
$db_database = 'MY_DATABASE_NAME';
$db_username = 'MY_DATABASE_USER';
$db_password = 'MY_DATABASE_PASSWORD';
$dsn = 'mysql:host=' . $db_hostname . ';dbname=' . $db_database . ';';

try
{
   $dbconn = new PDO($dsn, $db_username, $db_password);
   $dbconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
  echo 'Error connecting to database: ' . $e->getMessage();
}
?>

Inside the function where the error occurs I have the database connection defined as a superglobal as such ...

global $dbconn;

I am a bit confused as to what is happening since all worked well on my development machine. I was wondering if the PDO library was instal开发者_开发知识库led but from what I thought it was installed by default as part of PHP v5.

The script is running on an Ubuntu (5.0.51a-3ubuntu5.4) machine and the PHP version is 5.2.4. Thank you for any suggestions. I am really lost on this.


Always, but always, develop on the same platform as your production platform. Try your best to mirror the versions of server software (PHP, MySQL, etc). There will always be differences between installs, and especially in the way different OS platforms handle things. Use the old 'phpinfo()' script on each server to compare the differences, if any.

At any rate, even if both the Win and Linux platforms here have the exact same versions of everything, have you checked your configuration? In other words, is the MySQL host name in your DSN a local MySQL host (linked to your Win development platform), or the actual host the Ubuntu server uses? Or are they both "localhost" in either case? Double check this. Also, have you mirrored all data on both servers?

Do you know for a fact that the Ubuntu server has PDO? You essentially said you assume they are the same. Don't assume, verify.

As sobedai mentioned, look at the output of var_dump($dbconn), check that it is actually a PDO object, and go from there.

This is a typical PDO deployment for me:

// 'logger' is a custom error logging function with email alerts
try {
    $dbh= new PDO(dsn);
    if ( is_a($dbh, "PDO") ) {
      $sql= 'SELECT field FROM table';
      $stmt= $dbh->query($sql);
      if ( is_a($stmt, "PDOStatement") ) {
          // handle resultset
      }
      else {
          // weirdness, log it
          logger("Error with statement: {$sql}" .$dbh->errorCode());
      }
    }
    else {
      // some weirdness, log it
      logger('Error making connection: '. $dbh->errorCode());
    }
}
catch (PDOException $e) {
  logger('PDOException caught in '.__FILE__.' : '. $e->getMessage());
}
catch (Exception $e) {
  logger('General Exception caught in '.__FILE__.' : '. $e->getMessage());
}

Note I'm using PDO::errorCode in there, referencing the SQL-92 state codes.

Also, refer to the manual as much as possible.


Generally when this happens, the object you're trying to reference is not actually an instance of a class.

Since you've been developing on WAMP and testing on LAMP, include paths immediately come to mind.

Check the php includes path is correct on your LAMP stack. Check that all the necessary libs are installed (just do a quick phpinfo() page or you can use php -i from the command line).

And last but not least - do:

echo var_dump($dbconn);

confirm that it is indeed the object you think it is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜