0

嗨,我需要一些帮助来解决我不断收到的这个错误。它在脚本中的其他任何地方都可以正常工作,除非我尝试在这个函数中调用它。

我调用的函数是: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;

}

谢谢你的帮助。

4

3 回答 3

0

我的猜测是,由于此时尚未调用 mysql_con,因此 $g_mysql 变量不存在。

如果您尝试在非对象上调用方法,PHP 将触发您看到的错误。基本上由于变量不存在,它默认为空。

$a = null;
$a->prepare()

没有意义,所以这就是 PHP 抛出该错误的原因。

另一方面,你真的不应该使用全局变量。你可以谷歌或搜索 stackoverflow 以获得更详细的列表,但简而言之,它们破坏了代码的可移植性,并使此类错误更难发现。将一个额外的参数传递给每个需要它的函数调用是很痛苦的,但从设计的角度来看,它要好得多(而且实际上并没有太多额外的努力:p)。

于 2011-09-30T08:53:54.397 回答
0

该对象$g_mysqli应在您的代码中调用它之前进行初始化。

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

所以你不能在空对象中调用任何函数。

于 2012-08-07T09:00:55.503 回答
-1

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..

于 2013-04-07T18:51:59.857 回答