我正在尝试创建一个登录/注册系统,我不断收到错误: 警告:mysql_query()期望参数2是资源,在第8行的/home/roscapex/public_html/core/functions/users.php中给出整数
警告:mysql_result()需要至少2个参数,第8行/home/roscapex/public_html/core/functions/users.php中给出的参数为1
我的代码是:
<?php
function user_exists($username) {
$username = sanitize($username);
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");
return (mysql_result(mysql_query($query, 0) == 1) ? true : false);
}
?>
注意这可能是重复但我尝试了所有其他相关问题。
答案 0 :(得分:0)
您的代码中存在多个错误。让我注释一下:
<?php
function user_exists($username) {
$username = sanitize($username);
$query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"); // $query now contains the result of the query (a Resource)
// Here you try to run the resource *again* trough mysql_query, with with 2 parameters (which results in the warning). I am not quite sure what you intended.
return (mysql_result(mysql_query($query, 0) == 1) ? true : false);
}
?>
我修好了。
<?php
function user_exists($username) {
$username = sanitize($username); // This hopefully santizes the username. Better use mysql_real_escape_string($username)
$query = "SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'";
$result = mysql_query($query) or die(mysql_error()); // Check if query was successful
$row = mysql_fetch_array($result); // fetch the first row
return ($row[0] > 1); // If there is one or more users with this name, return true.
}
// omit the closing ?>, because it causes trouble, when there are any chars behind it (headers alreay sent error etc.)