Php 为什么mysqli num_行总是返回0?
我在使用mysqli获取要返回的行数时遇到了问题。我每次都会得到0,尽管肯定会有一些结果Php 为什么mysqli num_行总是返回0?,php,mysqli,Php,Mysqli,我在使用mysqli获取要返回的行数时遇到了问题。我每次都会得到0,尽管肯定会有一些结果 if($stmt = $mysqli->prepare("SELECT id, title, visible, parent_id FROM content WHERE parent_id = ? ORDER BY page_order ASC;")){ $stmt->bind_param('s', $data->id); $stmt->execute();
if($stmt = $mysqli->prepare("SELECT id, title, visible, parent_id FROM content WHERE parent_id = ? ORDER BY page_order ASC;")){
$stmt->bind_param('s', $data->id);
$stmt->execute();
$num_of_rows = $stmt->num_rows;
$stmt->bind_result($child_id, $child_title, $child_visible, $child_parent);
while($stmt->fetch()){
//code
}
echo($num_of_rows);
$stmt->close();
}
为什么它没有显示正确的号码?在查找数字行之前,您需要先呼叫:
if($stmt = $mysqli->prepare("SELECT id, title, visible, parent_id FROM content WHERE parent_id = ? ORDER BY page_order ASC;")){
$stmt->bind_param('s', $data->id);
$stmt->execute();
$stmt->store_result(); <-- This needs to be called here!
$num_of_rows = $stmt->num_rows;
$stmt->bind_result($child_id, $child_title, $child_visible, $child_parent);
while($stmt->fetch()){
//code
}
echo($num_of_rows);
$stmt->close();
}
if($stmt=$mysqli->prepare(“从内容中选择id、title、visible、parent\u id=?按页面排序ASC;”){
$stmt->bind_参数('s',$data->id);
$stmt->execute();
$stmt->store_result();num_行;
$stmt->bind_result($child_id、$child_title、$child_visible、$child_parent);
而($stmt->fetch()){
//代码
}
echo(行数);
$stmt->close();
}
查看上的文档,它在页面顶部附近(在主描述块中)显示。在$stmt上的var\u转储会产生什么?print\r()返回:mysqli\u stmt对象([受影响的行]=>-1[插入\u id]=>0[行数]=>0[参数计数]=>1[字段计数]=>4[错误]=>0[sqlstate]=>00000[id]=>1)确保数据存在…我理解它的方式(如果我错了,请纠正我,这对我来说是新的)是在调用$stmt->execute()后,执行查询,因此在此之后的任何时候我都应该能够访问检索到的行数?我尝试在$stmt->execute()和$stmt->fetch()之后回显$stmt->num_行。查询是绝对正确的,因为它正确地填充了表的单元格(//代码),所以有数据。谢谢