Php Mysqli->;num_行始终返回1

Php Mysqli->;num_行始终返回1,php,mysqli,Php,Mysqli,对于$item\u result->num\u行,似乎总是返回1;即使数据库中有0行。但是,如果项目存在,它会正确更新该行。我肯定我的语法有问题,但我很难理解这个mysqli $item_query = "SELECT COUNT(*) FROM `rel` WHERE `cart_id` = '".$cartId."' && `id_item` = '".$item_id."'"; $item_result = $mysqli->query($item_query) or

对于$item\u result->num\u行,似乎总是返回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');    

}

当您执行
选择COUNT(*)
时,将始终至少有一个结果。即使是0


您需要获取查询结果以获得正确的计数。

当您执行
选择计数(*)
时,将始终至少有一个结果。即使是0

您需要获取查询结果以获得正确的计数