问题
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.
回答1:
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.
$a = null;
$a->prepare()
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).
回答2:
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.
回答3:
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..
来源:https://stackoverflow.com/questions/7607997/php-call-to-a-member-function-prepare-on-a-non-object-error