开发者

Display Message if num_rows = 0 in php from mysqli prepared-statement [duplicate]

This question already has an answer here: 开发者_运维技巧 Why does mysqli num_rows always return 0? (1 answer) Closed 1 year ago.

I am having an issue trying to get the number of rows from a prepared statement query in php, my query looks as follows:

$DBH = getDBH();
$stmt = $DBH->prepare("SELECT info FROM list WHERE tag = ?");
$stmt->bind_param("s",$tag);
$stmt->execute();
$stmt->bind_result($information);

and i basically just want to say if there is no result returned display "no result returned" can anyone please help?


You need to use $statement->store_result() first. Using your code:

$DBH = getDBH();
$stmt = $DBH->prepare("SELECT info FROM list WHERE tag = ?");
$stmt->bind_param("s",$tag);
$stmt->execute();
$stmt->store_result();
$num_rows = $stmt->num_rows;
$stmt->bind_result($information);


I don't recognize the class you are using, but generally I do this:

$sql = "SELECT * FROM dummy WHERE category=13";
$result = mysql_query($sql);
if(mysql_num_rows($result)>0) {
// execute for positive results
} else {
// execute for 0 rows returned.
}


$sql = "SELECT * FROM dummy WHERE category=13";
$result = mysqli_query($link, $sql);
$numrows=mysqli_num_rows($result)
if($numrows>0) {
// execute for positive results
} else {
// execute for 0 rows returned.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜