PHP在非对象错误时调用成员函数prepare() [英] PHP Call to a member function prepare() on a non-object error

查看:92
本文介绍了PHP在非对象错误时调用成员函数prepare()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要不断解决此错误的帮助.它可以在脚本中的其他所有地方正常工作,除非我尝试在此函数中调用它.

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.

我正在调用的函数是:getTotalServers

The function I am calling is: 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;

}

感谢您的帮助.

推荐答案

对象$g_mysqli应该在之前被初始化.

在您的情况下,您将在许多地方创建全局变量$g_mysqli,但每次在初始化之前它都是一个NULL对象.

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.

所以您不能在null对象内调用任何函数.

So you can't call any function within null object.

这篇关于PHP在非对象错误时调用成员函数prepare()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆