为什么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();

}

为什么没有显示正确的数字?


绝地无双
浏览 953回答 3
3回答

慕斯709654

您需要先调用MySqli_Stmt::store_result()num_rows查找:if($stmt = $mysqli->prepare("SELECT id, title, visible, parent_id FROM content WHERE parent_id = ? ORDER BY page_order ASC;")){&nbsp;&nbsp;&nbsp; &nbsp; $stmt->bind_param('s', $data->id);&nbsp;&nbsp;&nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; $stmt->store_result(); <-- This needs to be called here!&nbsp; &nbsp; $num_of_rows = $stmt->num_rows;&nbsp;&nbsp;&nbsp; &nbsp; $stmt->bind_result($child_id, $child_title, $child_visible, $child_parent);&nbsp;&nbsp;&nbsp; &nbsp; while($stmt->fetch()){&nbsp; &nbsp; &nbsp; &nbsp; //code&nbsp; &nbsp; }&nbsp; &nbsp; echo($num_of_rows);&nbsp; &nbsp; $stmt->close();}请参阅上的文档MySQLi_Stmt->num_rows,该文档显示在页面顶部附近(在主要说明区域中)...

万千封印

尝试$num_of_rows在if(statement)之后-在bind_param之前设置您的权限...除非那会改变您的结果。没有更多信息很难说。

素胚勾勒不出你

您的查询似乎出了点问题。尝试进行直接查询,例如if($stmt = $mysqli->query("SELECT id, title, visible, parent_id FROM content WHERE parent_id = YOURIDHERE ORDER BY page_order ASC;")){,看看是否仍然可以解决0个结果
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS
MySQL