在 index.php 中的非对象上调用成员函数 fetch()
Call to a member function fetch() on a non-object in index.php
$sql = $db->query('SELECT `xp`,`deposits`,`withdraws`,`zeros`,`wins`,`daily` FROM `users` WHERE `steamid` = ' . $db->quote($user['steamid']));
$row = $sql->fetch();
$profile['xp'] = $row['xp'];
$profile['deposits'] = $row['deposits'];
$profile['withdraws'] = $row['withdraws'];
$profile['zeros'] = $row['zeros'];
$profile['level'] = LevelFromXP($row['xp'])[0];
$profile['level_name'] = LevelFromXP($row['xp'])[1];
$profile['rank'] = RankFromWins($row['wins']);
$profile['bet_wins'] = $row['wins'];
if ($row['daily'] == "0") {
if (explode(" | ", $user['name'])[1] == "mysitehehe.com") {
$daily = 1;
} else {
$daily = -1;
}
} elseif ($row['daily'] < time()) {
$daily = 1;
} else {
$daily = 0;
$profile['dailywait'] = $row['daily'] - time();
}
$page = getTemplate('main.tpl', array('page' => $_GET['page'], 'db' => $db, 'user' => $user, 'profile' => $profile, 'daily' => $daily, 'happyhour' => $happyhour, 'settings' => $settings));
echo $page;
break;
为什么我会收到这个错误?
致命错误:在第 420
行 index.php 中的非对象上调用成员函数 fetch()
您的查询失败,因此 $sql
是布尔值 false
而不是您可以调用 fetch
.
的查询结果
使用结果前请检查查询是否成功
$sql = $db->query('SELECT `xp`,...');
if(!$sql){
//DB error: prob bad query or wrong connection credentials
die($sql->error);
}
elseif($sql->num_rows===0){
// 0 record found
}
else{
$row = $sql->fetch();
...
}
$sql = $db->query('SELECT `xp`,`deposits`,`withdraws`,`zeros`,`wins`,`daily` FROM `users` WHERE `steamid` = ' . $db->quote($user['steamid']));
$row = $sql->fetch();
$profile['xp'] = $row['xp'];
$profile['deposits'] = $row['deposits'];
$profile['withdraws'] = $row['withdraws'];
$profile['zeros'] = $row['zeros'];
$profile['level'] = LevelFromXP($row['xp'])[0];
$profile['level_name'] = LevelFromXP($row['xp'])[1];
$profile['rank'] = RankFromWins($row['wins']);
$profile['bet_wins'] = $row['wins'];
if ($row['daily'] == "0") {
if (explode(" | ", $user['name'])[1] == "mysitehehe.com") {
$daily = 1;
} else {
$daily = -1;
}
} elseif ($row['daily'] < time()) {
$daily = 1;
} else {
$daily = 0;
$profile['dailywait'] = $row['daily'] - time();
}
$page = getTemplate('main.tpl', array('page' => $_GET['page'], 'db' => $db, 'user' => $user, 'profile' => $profile, 'daily' => $daily, 'happyhour' => $happyhour, 'settings' => $settings));
echo $page;
break;
为什么我会收到这个错误? 致命错误:在第 420
行 index.php 中的非对象上调用成员函数 fetch()您的查询失败,因此 $sql
是布尔值 false
而不是您可以调用 fetch
.
使用结果前请检查查询是否成功
$sql = $db->query('SELECT `xp`,...');
if(!$sql){
//DB error: prob bad query or wrong connection credentials
die($sql->error);
}
elseif($sql->num_rows===0){
// 0 record found
}
else{
$row = $sql->fetch();
...
}