php – 为什么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,$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_parent);  

    while($stmt->fetch()){
        //code
    }

    echo($num_of_rows);

    $stmt->close();
}

请参阅the docs on MySQLi_Stmt->num_rows,它说它在页面顶部附近(在主要描述块)…

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...