选择后mysql php处理没有数据

时间:2014-03-21 10:32:09

标签: php mysql

我想知道在执行查询后如何处理无数据或错误代码1329。

$stmt = $this->db->prepare('SELECT Username FROM Users WHERE device_id=?');
$stmt->bind_param("s", $device_id);
$flag = $stmt->execute();
$stmt->bind_result($Username);

我希望这段代码能够通过,但是执行后,该标志为真。

1 个答案:

答案 0 :(得分:0)

我建议这样做:

$stmt = $this->db->prepare('SELECT Username FROM Users WHERE device_id=?');
$stmt->bind_param("s", $device_id);
$stmt->execute();
$stmt->bind_result($Username);
if($stmt->fetch()) {
    echo "Found $Username";
} else {
    echo "Did not find any user";
}
$stmt->close();