在我准备好的语句中,我可以检索MySQL返回的值,但如果MySQL不返回任何数据,则无法通知用户。
下面是我准备好的语句的代码。
$database是mysqli连接的对象。
$stmt = $database->prepare($qryCallProc);
$stmt->bind_param('iss',$userId,$trendType,$university);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($param1,$param2);
while($stmt->fetch) {
print_r($param1);
}
$stmt->free_result();
$stmt->close();我甚至在$stmt->bind_result($param1,$param2)之后尝试使用下面的代码,但没有用:
if(!$stmt->fetch()) {
print_r("there is no data");
} elseif (is_null($stmt->fetch())) {
print_r("there is no data");
}我是在代码中遗漏了什么,还是有其他方法可以这样做呢?
发布于 2014-10-07 06:17:59
您可以首先使用num_rows检查查询是否生成行:
if($stmt->num_rows > 0) {
$stmt->store_result();
$stmt->bind_result($param1,$param2);
while($stmt->fetch) {
echo $param1;
}
} else {
// sorry no rows found
}发布于 2018-11-07 11:40:09
在使用store_result()之前必须使用num_rows
您可以首先使用num_rows检查查询是否生成行:
$stmt->store_result();
if($stmt->num_rows > 0) {
$stmt->bind_result($param1,$param2);
while($stmt->fetch) {
echo $param1;
}
} else {
// sorry no rows found
}https://stackoverflow.com/questions/26229624
复制相似问题