一尘不染

为什么mysqli num_rows总是返回0?

php

我一直在获取要使用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();
}

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


阅读 268

收藏
2020-05-26

共1个答案

一尘不染

您需要先调用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;")){  
    $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,该文档显示在页面顶部附近(在主要说明区域中)…

2020-05-26