0

我正在尝试检查行是否存在。我正进入(状态

Commands out of sync; you can't run this command

我收到此错误是因为我添加了$stmt->store_result();如果我删除该行num_rows不会返回 true。如何检查该行是否存在?

$title = urldecode($_GET['product-tit/']);
$id = $_GET['item-id-pr'];
$mydb = new mysqli('localhost', 'root', '', 'database');

if(empty($title) || empty($_GET['item-id-pr'])){
header('Location: products.php');
exit();
}
else{
$stmt = $mydb->prepare("SELECT * FROM products where title = ? AND id = ? limit 1 ");
$stmt->bind_param('ss', $title, $id);
$stmt->execute();
$stmt->store_result();
?> 
<div>

<?php
if($stmt->num_rows < 1 )  {
     echo "not found";
     exit();


    } else{
$result = $stmt->get_result();
 echo $mydb->error;

 while ($row = $result->fetch_assoc()) {





echo wordwrap($row['price'], 15, "<br />\n", true); 
     exit();
}
$mydb->close ();}}
?>
</div>
4

1 回答 1

3

PHP 用户对行数的要求很奇怪。每个人都渴望得到它,而在 Web 开发中,真正需要它的情况很少,非常罕见。

说,这里我们实际上需要一些数据,而不是行数。但是使用这个数字只是为了看看我们是否有任何数据。它看起来不是很有趣/多余吗?如果我们已经拥有我们的数据 - 为什么我们需要任何额外的设施来查看我们是否拥有它?

<?
if(empty($_GET['product-tit']) || empty($_GET['item-id-pr'])){
    header('Location: products.php');
    exit();
}

$stmt = $mydb->prepare("SELECT * FROM products where title = ? AND id = ? limit 1 ");
$stmt->bind_param('ss', $_GET['product-tit'], $_GET['item-id-pr']);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();

if (!$row) { // <---- HERE it is! 
     echo "not found";
     exit();
}
?> 
<div>
于 2013-08-13T07:15:52.863 回答