$query = "SELECT 1 FROM users WHERE username = :username";
$query_params = array(':username' => $_POST['username']);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$row = $stmt->fetch();
if($row)
{
die("This username is already in use");
}
这一切都有效,但是:
如果查询只是
SELECT
or ,我真的需要准备好的语句SELECT COUNT
吗?
因为,如果INSERT / UPDATE / DELETE
表上没有任何操作 - 我想没有 sql 注入或垃圾邮件的危险?try/catch
每次去数据库时我真的需要声明吗?