获取返回的行数mysqli php

时间:2017-04-12 02:23:31

标签: php mysql mysqli prepared-statement

我一直试图解决这个问题。我厌倦了回应mysql查询中返回的行数

$linkTow  = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$getVotesSQL = "SELECT null FROM votes WHERE `idgame` = ?";
$getVotesStmt = $linkTow->prepare($getVotesSQL);
$getVotesStmt->bind_param("s", $idGame);
$getVotesStmt->execute();
echo $getVotesStmt->num_rows;
$getVotesStmt->close();
$linkTow->close();

我总是得到0.

1 个答案:

答案 0 :(得分:0)

如果您真的更喜欢使用$linkTow->prepare,那么这是正确的方法:

$getVotesSQL = "SELECT null FROM votes WHERE `idgame` = ?";
$getVotesStmt = $linkTow->prepare($getVotesSQL);
$getVotesStmt->bind_param("s", $idGame);
$getVotesStmt->execute();
$getVotesStmt->store_result(); // <- IMPORTANT
echo $getVotesStmt->num_rows;
$getVotesStmt->close();
$linkTow->close();

之前您正在做的是$linkTow->query

  

<强> MANUAL

http://php.net/manual/en/mysqli-stmt.num-rows.php