从昨天开始,我遇到的问题是fetch_assoc()仅被执行一次,此后,每次我调用fetch_assoc()时,返回值都是空。
有人可以找出问题所在吗? 如果我在phpmyadmin中进行查询,则预期结果不会为空。
$sent = $mysqli_chat->query("SELECT to_user_id FROM chat WHERE from_user_id='" . $_SESSION['id'] . "';");
$receive = $mysqli_chat->query("SELECT from_user_id FROM chat WHERE to_user_id='" . $_SESSION['id'] . "';");
while ($row = $receive->fetch_assoc()){
$contacts = $mysqli->query('SELECT vorname_eltern, nachname_eltern, vorname_lehrer, nachname_lehrer FROM login WHERE id="' . $row['from_user_id'] . '";');
$nachname = $contacts->fetch_assoc()['nachname_eltern'] . $contacts->fetch_assoc()['nachname_lehrer'];
$vorname = $contacts->fetch_assoc()['vorname_eltern'] . $contacts->fetch_assoc()['vorname_lehrer'];
$chats .= '<li class="list-group-item p-0 px-3 pb-2" style="border-top:0;" onclick="changeChat(' . $i++ . ')">
<img class="user-avatar rounded-circle mr-2" src="images/avatars/0.jpg" alt="User Avatar" width="40" height="40"><span>' . $vorname . ' ' . $nachname . '</span>
</li>';
$js_nachname .= "'" . $nachname . "', ";
$js_vorname .= "'" . $vorname . "', ";
}
答案 0 :(得分:1)
我假设您是在谈论$contacts->fetch_assoc()
。 $contacts
查询仅匹配单个行。每次调用$contacts->fetch_assoc()
时,它都会尝试返回下一行结果,但是没有下一行。对$contacts->fetch_assoc()
的第一次调用返回该行,随后的调用返回NULL
。
您应将行分配给变量,然后使用该变量,而不要重复调用fetch_assoc()
。
$contacts = $mysqli->query('SELECT vorname_eltern, nachname_eltern, vorname_lehrer, nachname_lehrer FROM login WHERE id="' . $row['from_user_id'] . '";');
$contact_row = $contacts->fetch_assoc();
$nachname = $contact_row['nachname_eltern'] . $contact_row['nachname_lehrer'];
$vorname = $contact_row['vorname_eltern'] . $contact_row['vorname_lehrer'];