你好我想将mysql函数转换为mysqli版本。此功能用于检查用户是否存在。所以我是功能新手,甚至是mysqli。这就是为什么我在改造时遇到问题。
mysql-version是:
function a($uname){
return (mysql_result(mysql_query("SELECT COUNT (`a`) FROM `table_1` WHERE `a`='$uname'"), 0) == 1) ? true : false;
}
myqli-version我认为是:
function a($uname){
return (mysqli_num_rows(mysqli->query("SELECT COUNT (`a`) FROM `table_1` WHERE `a`='$uname'"), 0) == 1) ? true : false;
}
我知道没有mysql_result了。我决定使用mysqli_num_rows。但这个功能不起作用,我不知道为什么。 error_reporting已启用,但在调用页面时,我将收到一个没有错误消息的空白页面?!
所以,如果有人可以帮助我,我真的很感激。
非常感谢。
答案 0 :(得分:0)
免费documentation:
mysqli->query
返回查询的mysqli_result对象,实际返回一些结果,失败查询返回'false',所有其他查询返回'true'(如果成功)。
除非您更彻底地检查结果,否则您不会对查询结果有任何了解。
尝试这样的事情:(这假设连接已成功建立,并且$ uname已被正确转义。)
function a($uname) {
$found = false;
$result = mysqli->query("SELECT `a` FROM `table_1` WHERE `a`='$uname'");
if($result) {
//We used a 'SELECT' query and the query was successful. That means, we now have a mysqli_result object.
$found = ($result->num_rows == 1); //Determines, if something was actually found or not.
$result->close(); //Frees some ressources. Not necessary, but doesn't hurt either.
} else { //The query failed, as such we have nothing to evaluate.
die("Queryerror: ".mysqli->error);
}
return $found;
}
我将查询参数从'COUNT'更改为'a',否则'num_rows'将始终返回1,即使实际计数为'0'。这样,它返回匹配行的数量,基本上为'不匹配'返回'0'或'匹配'为'1'。
答案 1 :(得分:0)
你需要一个辅助函数来将所有脏的mysqli内部结构放在幕后 像safemysql一样。它也可以模仿mysql_result:
function a($uname){
global $db; // with mysqi you have to connect once and then use this connection
return (bool)$db->getOne("SELECT COUNT (1) FROM table_1 WHERE a=?s"), $uname);
}
并且可以解决许多类似的问题。