2
這是關於一個職位Show three images from each user,其中有一個很好的解決方案,我遇到了同樣的問題。mysqli獲取記錄
if ($stmt = $mysqli->prepare("SELECT p1.userID, p1.picture as pic1, p2.picture as pic2, p3.picture as pic3
FROM
pictures p1 left join pictures p2
on p1.userID=p2.userID and p1.picture<>p2.picture
left join pictures p3
on p1.userID=p3.userID and p1.picture<>p3.picture and p2.picture<>p3.picture
GROUP BY p1.userID
LIMIT ?,1")) {
$stmt->bind_param("i", $first);
$stmt->execute();
$stmt->bind_result($user, $pic1, $pic2, $pic3);
$stmt->fetch();
$stmt->close();
}
$mysqli->close();
?>
<div style="position:absolute; top:50px; left:100px; width:800px; text-align: center;">
<img src="<?PHP echo (isset($pic1) ? $image_path.$pic1 : $no_image); ?>" width="176px" height="197px">
<img src="<?PHP echo (isset($pic2) ? $image_path.$pic2 : $no_image); ?>" width="176px" height="197px">
<img src="<?PHP echo (isset($pic3) ? $image_path.$pic3 : $no_image); ?>" width="176px" height="197px">
</div>
我想知道如何也可以在此查詢中獲取pictureID?例如,如果我使用圖片進行投票,那麼我還需要獲取pictureID。 Supericy幫助後
答:(如果有人找相同)
if ($stmt = $mysqli->prepare("SELECT p1.userID, p1.picture as pic1, p1.pictureID as pic1id, p2.picture as pic2, p2.pictureID as pic2id, p3.picture as pic3, p3.pictureID as pic3id
FROM
pictures p1 left join pictures p2
on p1.userID=p2.userID and p1.picture<>p2.picture
left join pictures p3
on p1.userID=p3.userID and p1.picture<>p3.picture and p2.picture<>p3.picture
GROUP BY p1.userID
LIMIT ?,1")) {
然後將它綁定
$stmt->bind_result($user, $pic1, $pic1id, $pic2, $pic2id, $pic3, $pic3id);
聽起來不錯,但我接下來要它的變量綁定以及在bind_result? –
哦,是的。 '$ stmt-> bind_result($ user,$ pic1,$ pic1_id,$ pic2,$ pic2_id,$ pic3,$ pic3_id);'(編輯我的答案) – Supericy
我試過了,但它給出了「Warning:mysqli_stmt :: bind_result() [mysqli-stmt.bind-result]:綁定變量的數量與預編譯語句中的字段數量不匹配「 –