PHP 在发生非对象错误时调用成员函数prepare()
您好,我需要一些帮助来解决我不断收到的错误。除了当我尝试在这个函数中调用它时之外,它在脚本中的其他任何地方都工作得很好。
我正在调用的函数是: getTotalServers
错误:
Fatal error: Call to a member function prepare() on a non-object in C:\xampp2\htdocs\runeloft\ss_sources\util.php on line 208
在此函数中调用:
function display_table($online, $where, $num_servers = null){
global $aaaa, $bbbb;
$num_per_page = 30;
$page = 1;
// how many records per page
$size = 10;
// we get the current page from $_GET
if (isset($_GET['page'])){
$page = (int) $_GET['page'];
}
$aaaa = getTotalServers(1, $num_servers);;
$bbbb = $page;
// create the pagination class
$pagination = new Pagination();
$pagination->setLink("?action=status&page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($num_servers);
global $g_headers;
$g_headers = array(
'name' => 'Server Name',
'ip' => 'IP',
'port' => 'Port',
'uptime' => 'Uptime',
'online' => 'Status',
);
$start = isset($_GET['start']) ? $_GET['start'] : 0;
mysql_con();
global $g_mysqli;
$start = $g_mysqli->real_escape_string($start);
if(isset($_GET['sort']) && isset($g_headers[$_GET['sort']])){
$order_by = 'ORDER BY `'.$g_mysqli->real_escape_string($_GET['sort']).'` '.(isset($_GET['desc']) ? 'DESC' : 'ASC');
}else{
//default sort
$order_by = 'ORDER BY `uptime` DESC, `time` ASC';
}
//$order_by .= " LIMIT $start, $num_per_page";
$order_by .= " " . $pagination->getLimitSql();
if($start == 0 && $online == 1 && !isset($_GET['sort']))
echoTable('Spons', "`sponsored` != '0'", "ORDER BY `sponsored` DESC, RAND() LIMIT 10");
echoTable('Other', $where, $order_by, $online, $start, $num_per_page, $num_servers);
close_mysql();
}
被调用函数:
function getTotalServers($online, $where, $num_servers = null){
$where = "`online` = '$online' AND `sponsored` = '0'";
if($num_servers == null){
global $g_mysqli;
$stmt = $g_mysqli->prepare("SELECT COUNT(*) FROM `servers` WHERE ".$where) or debug($g_mysqli->error);
$stmt->execute();
// bind result variables
$stmt->bind_result($num_servers);
$stmt->fetch();
$stmt->close();
}
return $num_servers;
}
感谢您的帮助。
Hi i need some help with this error I keep getting. It works fine everywhere else in the script except when I try to call it within this one function.
The function I am calling is: getTotalServers
Error:
Fatal error: Call to a member function prepare() on a non-object in C:\xampp2\htdocs\runeloft\ss_sources\util.php on line 208
Calling in this function:
function display_table($online, $where, $num_servers = null){
global $aaaa, $bbbb;
$num_per_page = 30;
$page = 1;
// how many records per page
$size = 10;
// we get the current page from $_GET
if (isset($_GET['page'])){
$page = (int) $_GET['page'];
}
$aaaa = getTotalServers(1, $num_servers);;
$bbbb = $page;
// create the pagination class
$pagination = new Pagination();
$pagination->setLink("?action=status&page=%s");
$pagination->setPage($page);
$pagination->setSize($size);
$pagination->setTotalRecords($num_servers);
global $g_headers;
$g_headers = array(
'name' => 'Server Name',
'ip' => 'IP',
'port' => 'Port',
'uptime' => 'Uptime',
'online' => 'Status',
);
$start = isset($_GET['start']) ? $_GET['start'] : 0;
mysql_con();
global $g_mysqli;
$start = $g_mysqli->real_escape_string($start);
if(isset($_GET['sort']) && isset($g_headers[$_GET['sort']])){
$order_by = 'ORDER BY `'.$g_mysqli->real_escape_string($_GET['sort']).'` '.(isset($_GET['desc']) ? 'DESC' : 'ASC');
}else{
//default sort
$order_by = 'ORDER BY `uptime` DESC, `time` ASC';
}
//$order_by .= " LIMIT $start, $num_per_page";
$order_by .= " " . $pagination->getLimitSql();
if($start == 0 && $online == 1 && !isset($_GET['sort']))
echoTable('Spons', "`sponsored` != '0'", "ORDER BY `sponsored` DESC, RAND() LIMIT 10");
echoTable('Other', $where, $order_by, $online, $start, $num_per_page, $num_servers);
close_mysql();
}
Called Function:
function getTotalServers($online, $where, $num_servers = null){
$where = "`online` = '$online' AND `sponsored` = '0'";
if($num_servers == null){
global $g_mysqli;
$stmt = $g_mysqli->prepare("SELECT COUNT(*) FROM `servers` WHERE ".$where) or debug($g_mysqli->error);
$stmt->execute();
// bind result variables
$stmt->bind_result($num_servers);
$stmt->fetch();
$stmt->close();
}
return $num_servers;
}
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的猜测是,由于此时尚未调用 mysql_con,因此 $g_mysql 变量不存在。
如果您尝试调用非对象上的方法,PHP 将触发您所看到的错误。基本上,由于该变量不存在,因此默认为 null。
毫无意义,这就是 PHP 抛出该错误的原因。
另一方面,您确实不应该使用全局变量。您可以通过 google 或 stackoverflow 搜索更详细的列表,但简而言之,它们破坏了代码的可移植性,并使像这样的错误更难发现。将额外的参数传递给每个需要它的函数调用是很痛苦的,但从设计的角度来看它要好得多(并且实际上不需要太多额外的努力:p)。
My guess is that as mysql_con has not been called at that point, the $g_mysql variable does not exist.
If you try to call a method on a non-object, PHP will trigger the error you are seeing. Basically since the variable doesn't exist, it's defaulting to null.
Makes no sense, so that's why PHP is throwing that error.
On a different note, you really shouldn't use global variables. You can google or search stackoverflow for a more detailed list, but in short, they break portability of code, and make errors like this one harder to spot. It's a pain to pass an extra parameter to each function call that needs it, but it's much better from a design point of view (and really not much extra effort :p).
对象
$g_mysqli
应该在在代码中调用之前进行初始化。在您的情况下,您在许多地方创建全局变量
$g_mysqli
但每次在初始化之前它都是 NULL 对象。所以你不能调用空对象中的任何函数。
The object
$g_mysqli
should be initialized BEFORE it is called in your code.In your case you are creating global variable
$g_mysqli
at many places but every time it is a NULL object before you initialize it.So you can't call any function within null object.
问题出在 header() 函数上。
您需要做的就是在任何使用 header() 函数的页面中的代码顶部添加 ob_start() 。
只是一个令人困惑的错误!
如果有人知道原因请解释一下..
The problem is with header() function.
The all you need to do is to add a ob_start() at the top of your codes in any page which uses header() function.
Just a confusing error!!!
If anyone knows the reason please explain..