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

发布于 2024-08-26 18:44:23 字数 1120 浏览 4 评论 0原文

我用 PHP 编写了一个脚本,它从 MySQL 数据库中读取数据。我是 PHP 新手,一直在使用 PDO 库。我一直在使用 WAMP Server 2 的 Windows 机器上进行开发,一切都运行良好。但是,当我将脚本上传到将使用该脚本的 LINUX 服务器时,运行脚本时出现以下错误。

Fatal error: Call to undefined function query()

这是发生错误的行...

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

变量 $dbconn 首先在我的 dblogin.php 包含文件中定义,我在下面列出了该文件。

<?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();
}
?>

在发生错误的函数内部,我将数据库连接定义为超全局连接...

global $dbconn;

我对发生的事情有点困惑,因为一切都在我的开发计算机上运行良好。我想知道 PDO 库是否已安装,但我认为它是作为 PHP v5 的一部分默认安装的。

该脚本在 Ubuntu (5.0.51a-3ubuntu5.4) 机器上运行,PHP 版本为 5.2.4。感谢您的任何建议。我真的对此迷失了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话难免假 2024-09-02 18:44:23

始终,但始终,在与您的生产平台相同的平台上进行开发。尽量镜像服务器软件的版本(PHP、MySQL等)。安装之间总是存在差异,尤其是不同操作系统平台处理事物的方式。使用每个服务器上旧的“phpinfo()”脚本来比较差异(如果有)。

无论如何,即使这里的Win和Linux平台都有完全相同的版本,你检查过你的配置吗?换句话说,你的DSN中的MySQL主机名是本地MySQL主机(链接到你的Win开发平台),还是Ubuntu服务器使用的实际主机?或者在任何一种情况下它们都是“localhost”?仔细检查一下。另外,您是否镜像了两台服务器上的所有数据?

你知道 Ubuntu 服务器有 PDO 吗?您实质上是说您假设它们是相同的。不要假设,要验证。

正如 sobedai 提到的,查看 var_dump($dbconn) 的输出,检查它实际上是一个 PDO 对象,然后从那里开始。

这对我来说是一个典型的 PDO 部署:

// '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());
}

注意我在那里使用 PDO::errorCode,引用 SQL-92 状态代码

另外,请尽可能参考手册

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.

久隐师 2024-09-02 18:44:23

通常,当发生这种情况时,您尝试引用的对象实际上并不是类的实例。

由于您一直在 WAMP 上进行开发并在 LAMP 上进行测试,因此立即想到包含路径。

检查 LAMP 堆栈上的 php include 路径是否正确。
检查是否安装了所有必需的库(只需执行快速 phpinfo() 页面,或者您可以从命令行使用 php -i )。

最后但并非最不重要的一点 - 一定要做:

echo var_dump($dbconn);

确认它确实是您认为的对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文