php 无法获取 mysqli

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8933331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:48:28  来源:igfitidea点击:

Couldn't fetch mysqli

mysqlnginxmysqliphp

提问by James Dawson

I'm running an nginx server with PHP-FPM and MySQL. PHP has the MySQL and MySQLi extensions installed as confirmed by phpinfo(). I uploaded my scripts which work perfectly on localhost, and I'm getting a 500 error when I try and load the page. My nginx logs show this:

我正在运行带有 PHP-FPM 和 MySQL 的 nginx 服务器。PHP 安装了 MySQL 和 MySQLi 扩展,如phpinfo(). 我上传了在本地主机上完美运行的脚本,但在尝试加载页面时出现 500 错误。我的 nginx 日志显示:

2012/01/19 22:01:27 [error] 3393#0: *14 FastCGI sent in stderr: "PHP Warning: mysqli::prepare(): Couldn't fetch mysqli in /var/www/mydomain.com/chat/index.php on line 12

PHP Fatal error: Call to a member function execute() on a non-object in /var/www/mydomain.com/chat/index.php on line 13" while reading response header from upstream, client: 82.32.181.151, server: mydomain.com, request: "GET /chat/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "mydomain.com"

2012/01/19 22:01:27 [错误] 3393#0: * 14 FastCGI 在标准错误中发送:“PHP 警告:mysqli::prepare():无法在 /var/www/mydomain.com/ 中获取 mysqli第 12 行的 chat/index.php

PHP 致命错误:在第 13 行的 /var/www/mydomain.com/chat/index.php 中的非对象上调用成员函数 execute(),同时从上游读取响应标头,客户端:82.32.181.151,服务器:mydomain.com,请求:“GET /chat/HTTP/1.1”,上游:“fastcgi://127.0.0.1:9000”,主机:“mydomain.com”

I've replaced my actual domain with mydomain.com. On line 12 of index.php I have this:

我已经用mydomain.com. 在 index.php 的第 12 行我有这个:

$stmt = $mysqli->prepare('SELECT r_id, name, room_pass, max_users FROM `rooms` ORDER BY name ASC');

After Google'ing, I've found a couple of causes. One is that I prematurely closed my database connection, which isn't it. The other is that I've mixed OOP and functional MySQLi calls, which also isn't the problem. Also, my database connection information is correct.

在谷歌搜索之后,我发现了几个原因。一是我过早地关闭了我的数据库连接,不是这样。另一个是我混合了 OOP 和功能性 MySQLi 调用,这也不是问题。另外,我的数据库连接信息是正确的。

So, I'm stumped.

所以,我很难过。

回答by Marc B

I'm assuming your line 13 is actually this:

我假设你的第 13 行实际上是这样的:

$stmt->execute();

which means that your prepare call has failed and returned a boolean FALSE to $stmt. Since a boolean false is not an object, you can't call methods on it, hence the error.

这意味着您的准备调用失败并返回了一个布尔值 FALSE 到$stmt. 由于布尔 false 不是对象,因此您无法对其调用方法,因此会出现错误。

Try making the code:

尝试制作代码:

$stmt = $mysqli->prepare(...);
if ($stmt === FALSE) {
    die($mysqli->error);
}
$stmt->execute();

回答by Nanne

The problem is on your line 13. There you probably do something like

问题出在你的第 13 行。你可能会做类似的事情

$stmt->execute();

But now the variable you ask to contain a method executeisn't an object. Problems that could be your cause are:

但是现在您要求包含方法的变量execute不是对象。可能是您的原因的问题是:

  1. you typed $stmtwrong, so it is an uninitialized var
  2. Your query is wrong, so you've got false instead of a prepared statement
  3. Your db-connection doesn't work, so your $mysqli->preparefails.
  1. $stmt打错了,所以它是一个未初始化的 var
  2. 你的查询是错误的,所以你得到的是 false 而不是准备好的语句
  3. 你的数据库连接不起作用,所以你$mysqli->prepare失败了。

Figure out which one it is by using the error functions of mysqli...

使用mysqli的错误函数找出是哪一个...