我刚刚切换了我的webhoster并尝试在那里部署我的PHP应用程序。不幸的是,它在登录过程中崩溃并显示以下错误消息:
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中运行没有任何问题。
我知道这个答案被多次询问但我无法解决这个问题,因为几小时没有答案解决了我的问题。
答案 0 :(得分:1)
您需要在语句上调用fetch而不是$ result
while ($row = $stmt->fetch()) {
...
}
答案 1 :(得分:1)
var_dump
结果是mysqli
的实例,如果它是结果集,则它将是mysqli_result
的实例。
您没有获得结果的原因是您没有转移结果集。在您的情况下,$result
只是$stmt->execute()
的返回值,即boolean
。
您需要做的是在执行语句后获得结果。
$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);
$stmt->bind_param('s', $username);
$stmt->execute();
//put the results in to the $result variable
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
...
}
答案 2 :(得分:0)
$ stmt-&GT;执行(); $ result = $ stmt-&gt; get_result(); while($ row = $ result-&gt; fetch_assoc()){...} 如果你得到错误:致命错误:调用未定义的方法mysqli_stmt :: get_result():它需要mysqlnd驱动程序......如果它没有安装在你的网站空间,你将不得不使用BIND_RESULT&amp;取! 链接以完成http://php.net/manual/en/mysqli-stmt.get-result.php http://www.php.net/manual/en/mysqli-stmt.bind-result.php http://www.php.net/manual/en/mysqli-stmt.fetch.php