-2

我正在尝试创建登录/注册系统,但我不断收到错误消息:警告:mysql_query() 期望参数 2 是资源,在第 8 行的 /home/roscapex/public_html/core/functions/users.php 中给出的整数

警告:mysql_result() 至少需要 2 个参数,1 个在 /home/roscapex/public_html/core/functions/users.php 的第 8 行给出

我的代码是:

<?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);

}
?>

注意这可能是重复的,但我尝试了所有其他相关问题。

4

1 回答 1

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.)
于 2013-02-21T15:16:12.083 回答