douzhuangxuan3268 2016-08-31 23:26
浏览 77
已采纳

使用预准备语句时,num_rows()始终返回0 [重复]

This question already has an answer here:

I've tried looking at every one else's stack overflow with exact error in title, but I still can't figure this out.

I'm using http://andrew-greig.net/portfolio/better_mysqli.htm for mysqli extend because it does make binding items as arrays much easier. I been trying to move over from native mysql to mysqli and I've always had this same issue with tring to find out if there is a result or not.

Here is my current code

$searchSQL = $mysqli->select("SELECT * FROM coreUsers WHERE Username = ? AND Password = ?", $row, array($username, $password));
var_dump($searchSQL);

while($searchSQL->fetch())
    {

        echo "<br/>---FOUND Valid LOGIN!!</br>";

        exit;
    }   

The output I get is..

    object(mysqli_stmt)#2 (10) { ["affected_rows"]=> int(-1) ["insert_id"]=> 
int(0) ["num_rows"]=> int(0) ["param_count"]=> int(2) ["field_count"]=> int(19) 
["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } 
["sqlstate"]=> string(5) "00000" ["id"]=> int(1) } 
    ---FOUND Valid LOGIN!!

so it found the record, if I output the record information it does echo out Username and Password just fine from the row response, but num_rows is always 0. For this I can never get

$rowCount = $searchSQL->num_rows();

to work when trying to see if there are actual results before doing the while loop.

----EDIT----

I have no idea why this is marked as a duplicate. The post added refers to "boolean" and that isn't my error. Mine is object given.

if ($searchSQL === false) {
echo "invalid";
} else {
echo "valid";
}

this code doesn't work (from the post you marked as duplicate) because my response always will return true if you actually would look at the code I'm using from the extend.

</div>
  • 写回答

1条回答 默认 最新

  • dpp10181 2016-09-08 12:58
    关注

    As mentioned by @Ghost below the issue I was missing was 1 line of code.

     $stmt->store_result();
     $rowCount = $stmt->num_rows;
    

    This has to be called BEFORE I can access num_rows() on a prepared statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机数字电压表电路组成及框图
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line