首先,我们使用支持预准备语句的MySQL库 - 否则,我们将来会遇到SQL注入问题。现在回到实际的问题/答案。
如果我们使用MySQLi,我们有一些功能可以帮助我们。这里有一个回答你的问题瓦特/代码注释,以帮助穿过它的一个例子:
// create our db connection
$mysqli = new mysqli('localhost', 'db_username', 'db_password', 'db_table');
// create a Prepared Statement to query to db
$stmt = $mysqli->prepare('SELECT hashed_password FROM users WHERE username = ? LIMIT 1');
// dynamically bind the supplied "username" value
$stmt->bind_param('s', $username);
// execute the query
$stmt->execute();
// get the first result and store the first column in the `$hashed_password` variable
$stmt->bind_result($hashed_password);
$stmt->fetch();
// close our Prepared Statement and the db connection
$stmt->close();
$mysqli->close();
echo $hashed_password;
退房的PHP文件为mysqli::prepare()
更多的例子=]
注:我高度建议避免mysql_query()
(和家庭)功能。它们不仅被弃用,而且使用起来也很不安全。
'$ query =“SELECT hashed_password”; $ query。=“FROM users”; $ query。=“WHERE username ='{$ username}'”; $ query。=“LIMIT 1”;' –
当你运行你已经拥有的代码时会发生什么?你有错误吗?它提供的信息? –
但是,使用MySQL扩展已经过去了:现在已经快到2016年了,您应该使用预处理语句切换到MySQLi或PDO并绑定变量 –