有另一個PHP問題可能是簡單的,但我真的很感謝任何幫助!PHP MySQLi問題與準備SELECT變量不綁定
我的函數執行一個mysqli計數,但它綁定到的$ count參數沒有更改,即使sql運行良好並且沒有錯誤。
功能:
function itemsCount ($conn, $list_id) {
$count = 0;
if (!($stmt = $conn->prepare("SELECT count(uid) FROM collection.user_list_item WHERE list_id = ?"))) {
echo "Prepare failed: " . $mysqli->error;
}
if (!($stmt->bind_param('i', $list_id))) {
echo "Bind failed: " . $stmt->error;
}
if (!($stmt->execute())) {
echo "Execute failed: " . $stmt->error;
}
if (!($stmt->bind_result($count))) {
echo "Bind failed: " . $stmt->error;
}
echo $count;
$stmt->close();
return $count;
}
和呼叫:
public function doAdd ($conn, $list_id, $item_id) {
if ($this->itemsCount($conn, $list_id) < 20) {
...do stuff ...
}
}
對不起代碼不是很漂亮,但請幫我指出了正確的方向!
您還需要在'$ stmt-> bind_result()'之後調用'$ stmt-> fetch()'來實際檢索一行。 –
謝謝@MichaelBerkowski!我現在感覺非常愚蠢...... –