簡體   English   中英

Mysqli-> num_rows總是返回1

[英]Mysqli -> num_rows always returns 1

對於$ item_result-> num_rows,這似乎總是返回1; 即使數據庫中有0行。 但是,如果存在一項,它將正確更新該行。 我敢肯定我的語法有問題,但是我很難理解這個mysqli。

$item_query = "SELECT COUNT(*) FROM `rel` WHERE `cart_id` = '".$cartId."' && `id_item` = '".$item_id."'";
$item_result = $mysqli->query($item_query) or die($mysqli->error.__LINE__);





if($item_result->num_rows==1) {


$item_query2 = "SELECT * FROM `rel` WHERE `cart_id` = '".$cartId."' && `id_item` = '".$item_id."'";
$item_result2 = $mysqli->query($item_query2) or die($mysqli->error.__LINE__);

$getOldItems = $item_result2->fetch_array();
$oldQty = $getOldItems['amount'];
$oldNotes = $getOldItems['notes'];

$newQty = $oldQty + $item_quantity;
$newNotes = $oldNotes . $item_notes;


$update_qty = $mysqli->query("UPDATE rel SET amount = '$newQty', notes = '$newNotes' WHERE `cart_id` = '$cartId' && `id_item` = '$item_id'");

if(!$update_qty){
    printf("Errormessage: %s\n", $mysqli->error);
}
  header('Location: ./ordernew.php');   


} else {

    $insert_cart_item = $mysqli->query("INSERT INTO rel (`email`, `cart_id`, `id_item`, `amount`, `notes`) VALUES ('$email', '$cartId', '$item_id', '$item_quantity', '$item_notes')");

if(!$insert_cart_item) {
printf("Errormessage: %s\n", $mysqli->error);
}
 header('Location: ./ordernew.php');    

}

當您執行SELECT COUNT(*) ,將始終至少有一個結果。 即使其為0。

您將需要獲取查詢結果以獲取正確的計數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM