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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器