php 致命错误:在非对象上调用成员函数 close()。MySQLi 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9722159/
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
Fatal error: Call to a member function close() on a non-object. MySQLi issue
提问by cosmicsafari
this is my first post so go easy on me ^_^
这是我的第一篇文章,所以放轻松吧^_^
I have been getting the following error when i uploaded to a live server. It works ok on localhost which i thought was strange. Any help would be greatly appreciated.
当我上传到实时服务器时出现以下错误。它在本地主机上运行正常,我认为这很奇怪。任何帮助将不胜感激。
Fatal error: Call to a member function close() on a non-object....
The line it refers to
它所指的行
$stmt->close();
The connection to the DB
与数据库的连接
$connection=new mysqli($MYSQL_HOST,$MYSQL_USER,$MYSQL_PASS,$DB)or die(mysqli_error($connection));
The class itself.
班级本身。
function getTimes(){ //this method just pulls the results of the query and returns them as an array
global $connection;
$route = $this->route;
$station = $this->station;
$day = $this->day;
// create a prepared statement
if ($stmt = $connection->prepare("select time from timetable where route=? and day=? and station=?")) {
$stmt->bind_param("sss", $route, $day, $station); // bind parameters for markers
$stmt->execute(); //execute query
$stmt->bind_result($col1); //bind result variables
while ($stmt->fetch()){
$results[]=$col1;
}
}
$stmt->close();//close statement
return $results;
}
采纳答案by Churk
You should put $stmt
into you if clause. There is a possiblity that if (false) and still get to your $stmt->close();
你应该把$stmt
你的if子句放进去。有一种可能性,如果 (false) 并且仍然到达您的$stmt->close();
回答by MrCode
Move the close()
call into your if statement, so that it will only be called if a $stmt
was successfully created.
将close()
调用移动到您的 if 语句中,以便只有在$stmt
成功创建a时才会调用它。
// create a prepared statement
if ($stmt = $connection->prepare("select time from timetable where route=? and day=? and station=?")) {
$stmt->bind_param("sss", $route, $day, $station); // bind parameters for markers
$stmt->execute(); //execute query
$stmt->bind_result($col1); //bind result variables
while ($stmt->fetch()){
$results[]=$col1;
}
$stmt->close();//close statement
}
I suspect on your local machine you don't have errors turned on, but on the server you upload to, errors are on.
我怀疑在你的本地机器上你没有打开错误,但在你上传到的服务器上,错误出现了。
The root issue to address here is the fact that prepare()
is failing. The problem there most likely is the database on the server is missing the timetable
table or that table is missing one or more fields route
, day
or station
. As said by cbuckley, check $connection->error
for the full error message.
这里要解决的根本问题prepare()
是失败的事实。最有可能的问题是服务器上的数据库缺少该timetable
表或该表缺少一个或多个字段route
,day
或者station
。正如 cbuckley 所说,检查$connection->error
完整的错误消息。
When you uploaded to the server, did you remember to also make any database structure changes on the server?
当您上传到服务器时,您是否还记得在服务器上进行任何数据库结构更改?
回答by deed02392
Your problem was that the $stmt
object was instantiated as part of an if
condition test. In the cases it failed, i.e. when it returns false, you were still trying to call ->close()
on it anyway. I moved the method call within the if
block.
您的问题是该$stmt
对象被实例化为if
条件测试的一部分。在它失败的情况下,即当它返回 false 时,你仍然试图调用->close()
它。我在if
块内移动了方法调用。
Now you need to add an else
clause to handle the fact that your script couldn't prepare the statement and given you say this works locally but not on your live server, I suggest there is some configuration difference causing a problem here. You need to turn on error handling with display_errors('1')
and error_reporting(E_ALL)
. Don't forget to turn these off before letting the world at your new script. :)
现在您需要添加一个else
子句来处理您的脚本无法准备语句的事实,并且假设您说这在本地有效但在您的实时服务器上无效,我建议存在一些配置差异导致这里出现问题。您需要使用display_errors('1')
和打开错误处理error_reporting(E_ALL)
。在让世界了解您的新脚本之前,不要忘记关闭这些功能。:)
function getTimes(){ //this method just pulls the results of the query and returns them as an array
global $connection;
$route = $this->route;
$station = $this->station;
$day = $this->day;
// create a prepared statement
if ($stmt = $connection->prepare("select time from timetable where route=? and day=? and station=?")) {
$stmt->bind_param("sss", $route, $day, $station); // bind parameters for markers
$stmt->execute(); //execute query
$stmt->bind_result($col1); //bind result variables
while ($stmt->fetch()){
$results[]=$col1;
}
$stmt->close();//close statement
}
return $results;
}