且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么mysqli num_rows总是返回0?

更新时间:2023-02-19 22:27:18

您需要调用 MySqli_Stmt::store_result() 在num_rows查找之前:

You need to call MySqli_Stmt::store_result() prior to the num_rows lookup:

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 上的文档,它说它就在页面顶部附近(在主要描述块中)...

See the docs on MySQLi_Stmt->num_rows, it says it right near the top of the page (in the main description block)...