php mysqli获取结果总是返回1

时间:2014-02-28 15:10:26

标签: php mysqli prepared-statement

这是我正在尝试使用的代码。我正在检查表中总行数(注册人数)。我的输出没有任何错误,我不知道我做错了什么。同样的事情昨天有效。上帝知道现在发生了什么

$query = "SELECT count(*) FROM team"; 
$result = $db->prepare($query);
$result->execute(); 
$number= $result->fetch(); 
print_r($number);
var_dump($result);

输出:

1 

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

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

$query = "SELECT count(1) FROM team";

if($stmt = $db->prepare($query)){
    $stmt->execute();
    $stmt->bind_result($number);
    $stmt->fetch();
    $stmt->free_result();
    $stmt->close();
}

echo $number;

$query = "SELECT * FROM team";

if($stmt = $db->prepare($query)){
    $stmt->execute();
    $stmt->store_result();
    $number = $stmt->num_rows;
    $stmt->free_result();
    $stmt->close();
}

echo $number;