为什么mysqli num_rows总是返回0?
我一直无法获得使用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(); $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(); }
为什么不显示正确的数字?
您需要在num_rows查找之前调用MySqli_Stmt::store_result()
:
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(); }
请参阅MySQLi_Stmt->num_rows
上的文档 ,它在页面顶部附近(在主要描述块中)说明…
尝试设置你的$num_of_rows
之后你的if(语句) – 之前bind_param …除非这是改变你的结果。 很难说没有更多的信息。