MySQLi等同于MySQL代码 [英] MySQLi Equivalent Of MySQL Code
本文介绍了MySQLi等同于MySQL代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您能给我相当于MySQLi的这段代码吗?无法正确处理.
Can you give me the equivalent of this code im MySQLi? Can't get it right.
<?php
if(mysql_num_rows(mysql_query("SELECT userid FROM users WHERE userid = '$userid'"))){
//code to be exectued if user exists
}
?>
编辑:请向我解释一下哪里出了问题?
Care to explain to me what is wrong?
$mysqli = new mysqli($host, $username, $pass, $db);
if ($mysqli->connect_error) {
die('The Server Is Busy. Please Try Again Later.');
}
$result = $mysqli->query("SELECT userid FROM users WHERE userid = '$userid'");
if ($result->num_rows) {
echo "<h1>AWESOME</h1>";
}
推荐答案
从面向对象的意义上讲,它应来自:
Well, in an OO sense, it would go from:
if(mysql_num_rows(mysql_query("SELECT userid FROM users WHERE userid = '$userid'"))){
//code to be exectued if user exists
}
收件人(假设数字用户名):
To (assuming numeric userid):
$result = $mysqli->query("SELECT userid FROM users WHERE userid = ".(int) $userid);
if ($result->num_rows) {
//code
}
到(假设字符串用户标识):
To (assuming string userid) :
$result = $mysqli->query("SELECT userid FROM users WHERE userid = '". $db->real_escape_string($userid) . "');
if ($result->num_rows) {
//code
}
至(假设准备好的语句):
To (assuming prepared statements) :
$stmt = $mysqli->prepare("SELECT userid FROM users WHERE userid = ?");
$stmt->bind_param('s', $userid);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows) {
//code
}
现在,假设您正在使用MySQLi的OOP版本(恕我直言,您应该使用它,因为它在许多方面都使生活更轻松).
Now, that's assuming you're using the OOP version of MySQLi (which you should be, IMHO, since it makes life easier in a lot of ways).
这篇关于MySQLi等同于MySQL代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文