[英]How to get number of results from a mysqli query?
So I'm doing this query:所以我在做这个查询:
'SELECT * FROM Album WHERE id = ?;'
using a prepare sql statement, and I was wondering how to get the number of results this query returns?使用准备 sql 语句,我想知道如何获取此查询返回的结果数? B/c since every album id is unique this query should only return 1 album and I want to make sure that its doing that.
B/c 因为每个专辑 id 都是唯一的,所以这个查询应该只返回 1 张专辑,我想确保它这样做。
$stmt = $mysqli->prepare('SELECT * FROM Album WHERE id = ?;');
$id = 2;
$stmt->bind_param('i', $id);
$executed = $stmt->execute();
$result = $stmt->get_result();
if($row = $result->fetch_assoc()){
$info['id'] = $row['id'];
$info['title'] = $row['title'];
$info['date_created'] = $row['date_created'];
$info['creator'] = $row['creator'];
}
// Send back the array as json
echo json_encode($info);
You can get it using $result->num_rows
:您可以使用
$result->num_rows
获取它:
if ($row = $result->fetch_assoc()) {
$info['id'] = $row['id'];
$info['title'] = $row['title'];
$info['date_created'] = $row['date_created'];
$info['creator'] = $row['creator'];
/* determine number of rows result set */
$row_cnt = $result->num_rows;
printf("Result set has %d rows.\n", $row_cnt);
}
You can find more details on PHP Documentation .您可以在PHP 文档 中找到更多详细信息。
I want to make sure that its doing that
我想确保它这样做
In your code you are already doing that.在您的代码中,您已经在这样做了。 The following condition
以下条件
if($row = $result->fetch_assoc()){
does exactly what you want.做你想要的。
SELECT COUNT(*) FROM Album WHERE id = ?;
Reference: https://dev.mysql.com/doc/refman/5.7/en/counting-rows.html参考: https : //dev.mysql.com/doc/refman/5.7/en/counting-rows.html
$query = $dbh->prepare("SELECT * FROM Album WHERE id = ?");
$query->execute();
$count =$query->rowCount();
echo $count;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.