あなたのコードには複数の間違いがあります。注釈を付けましょう:
<?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.)