我剛剛切換了webhoster,並嘗試在此處部署我的PHP應用程序。不幸的是,在與此錯誤消息在登錄過程中崩潰了:致命錯誤:調用非對象(mysqli)上的成員函數fetch_assoc()
Fatal error: Call to a member function fetch_assoc() on a non-object in /home/infobo9/www/xxx/login.php on line 41
這是代碼:
$DB = new DB;
$mysqli = $DB->getMySQLiObject();
$sql = "SELECT ios_worker.username, ios_worker.prename, ios_worker.lastname, ios_worker.partnerid_fk, ios_worker.workerid, ios_worker.password, role.rolename, role.roledescription, ios_partners.accessToAdminPortal FROM ios_partners join ios_worker on ios_worker.partnerid_fk = ios_partners.partnerid join workerrole on workerrole.workerid_fk = ios_worker.workerid join role on workerrole.roleid_fk = role.roleid WHERE ios_worker.username = ? LIMIT 1";
$stmt = $mysqli->prepare($sql);
$result = $stmt->bind_param('s', $username);
$result = $stmt->execute();
var_dump($mysqli);
if (!$result) {
echo $mysqli->error;
}
var_dump($result);
while ($row = $result->fetch_assoc()) { // <-- fails here
...
}
輸出:
Notice: Undefined index: source in /home/infobo9/www/xxx/login.php on line 23
object(mysqli)#2 (19) { ["affected_rows"]=> int(-1) ["client_info"]=> string(6) "5.5.40" ["client_version"]=> int(50540) ["connect_errno"]=> int(0) ["connect_error"]=> NULL ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["field_count"]=> int(9) ["host_info"]=> string(33) "xxx via TCP/IP" ["info"]=> NULL ["insert_id"]=> int(0) ["server_info"]=> string(10) "5.5.37-log" ["server_version"]=> int(50537) ["stat"]=> string(52) "Commands out of sync; you can't run this command now" ["sqlstate"]=> string(5) "HY000" ["protocol_version"]=> int(10) ["thread_id"]=> int(82066639) ["warning_count"]=> int(0) }
bool(true)
Fatal error: Call to a member function fetch_assoc() on a non-object in /home/infobo9/www/xxx/login.php on line 41
所以$mysqli
設置,並$result
是true
。我究竟做錯了什麼? 我也複製了查詢,它在PHPMyAdmin中沒有任何問題。
我知道這個答案被多次詢問,但我無法解決這個問題,因爲小時,沒有答案解決了我的問題。
謝謝。添加後,我得到這個錯誤'致命的錯誤:調用未定義的方法mysqli_stmt :: get_result()在'。我檢查了mysqli和mysqlnd驅動程序是否都存在,情況就是如此。任何建議? – doonot 2015-02-23 21:50:10
是的,擴展可能未啓用。檢查你的php.ini文件並確保'extension = php_mysqli_mysqlnd.dll'沒有註釋。 (重新啓動您的網絡服務器。)此外,此方法僅適用於PHP 5.3+。如果你不能使用該方法,那麼你仍然可以使用'bind_result()'和'fetch()' – Crackertastic 2015-02-23 21:58:45