我想知道将$variable传递给查询的安全或正确方法。我是PHP的新手,这就是为什么我要问这样的初学者问题。这是示例一和示例二,由于符号,哪一个是正确的和更安全的?
示例一:
//here is the line I am asking about. The $identification
$query = "SELECT * FROM `members` WHERE `username` = '$identification' LIMIT 1";
示例二:
//here is the line I am asking about. The $identification
$query = "SELECT * FROM `members` WHERE `username` = '" . $identification . "' LIMIT 1";
我不需要关于 PHP 4 或 5 或 PDO 的答案。我只需要知道什么是正确的:
这
'" . $identification . "'
或者这个
'$identification'
我建议您使用 PDO 而不是 mysqli 扩展(适用于 php 5.1 及更高版本)
http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059
怎么样
$query =
"SELECT * FROM `members` WHERE `username` = '" .
mysql_escape_string($identification) .
"' LIMIT 1";
http://php.net/manual/en/function.mysql-escape-string.php
但是,mysql_escape_string
已被弃用。如果可以,您应该使用mysql_real_escape_string
http://php.net/manual/en/function.mysql-real-escape-string.php