普通网友 2010-03-30 18:40
浏览 344
已采纳

调用未定义的函数query() - 在PHP中使用PDO

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 installed 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.

  • 写回答

2条回答 默认 最新

  • doula2426 2010-03-30 19:03
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料