我有一个问题从mysql转换为mysqli,我已经尝试了php文档说但我仍然无法得到它。任何帮助将不胜感激。
return (mysql_result(mysql_query("SELECT COUNT(id) FROM users WHERE (username = '$username' OR email = '$username') AND password = '$password'"), 0) == 1) ? $user_id : false;
我试过了:
return (mysqli_data_seek(mysqli_query($con,"SELECT COUNT(id) FROM users WHERE (username = '$username' OR email = '$username') AND password = '$password'"), 0) == 1) ? $user_id : false;
整个功能:
function login($username, $password){
$con=mysqli_connect("127.0.0.1","root","","frostbase");
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
return (mysqli_data_seek(mysqli_query($con,"SELECT COUNT(id) FROM users WHERE (username = '$username' OR email = '$username') AND password = '$password'"), 0) == 1) ? $user_id : false;
mysqli_close($con);
}
答案 0 :(得分:0)
你可以做得更好。
safeMysql是一个工具,可以帮助您将丑陋的旧的基于mysql API的代码转换为新的丑陋的mysqli基于API的代码,但使用更好的方法。
关键是您不得在应用程序代码中使用原始API调用,无论是mysql
,mysqli
还是PDO
。但将它们包装在一个库中,该库将执行所有必需的操作,如数据绑定,提取,错误处理,分析等等。
坦率地说,你只需编写查询 - 其余的由lib完成:
$sql = "SELECT 1 FROM users WHERE (username = ?s OR email = ?s) AND password = ?s";
return (bool)$db->getOne($sql, $username, $username, $password);
或以函数的形式
function login($username, $password){
global $con; // you have to connect ONCE per application
$sql = "SELECT 1 FROM users WHERE (username = ?s OR email = ?s) AND password = ?s";
return (bool)$con->getOne($sql, $username, $username, $password);
}