2

I'm going through a vid tutorial on creating a user reg & login form in php and mysql.

The following script should echo 'exists' on the login.php form, however just shows a blank (indicating the user does not exist, when it does and s in fact the only username on the DB)

<?php
    function user_exists($username) {
   $username = sanitize($username);
   $query = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"); 
   return (mysql_result($query, 0) == 1) ? true : false;
 }
?>

The login form portion of code goes as follows:

if (user_exists('andy') === true) {
echo 'exists';
}
die();

Am I missing some syntax or something obvious?? here is a link to the vid tutorial if it helps http://www.youtube.com/watch?v=Til3oVNlho4

4

2 回答 2

0

如果您有一个空白屏幕,则可以添加回显语句以查看正在发生的事情。

function user_exists($username) {        echo '|Input Username=$username ';
  $username = sanitize($username);       echo '|Sanitized=$username ';
  $query = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"); 
                                         echo '|output any mysql_error '.mysql_error();
  $numberOfRows = mysql_num_rows($query); echo '|Number of Rows=$numberOfRows ';
  $hasRows = ($numberOfRows > 0);         echo '|Has rows=$hasRows ';
  //used explicit parentheses around logic for clarity...and again next line
  $exists = ($hasRows) ? 1 :0;
  //or just do this...
  $exists = $hasRows;
  return $exists;     
}
于 2013-07-18T14:21:01.390 回答
0

为什么不这样做呢?

<?php
function user_exists($username) {
$username = sanitize($username);
$query = mysql_query("SELECT * FROM `users` WHERE `username` = '$username'"); 
$exists = mysql_num_rows($query) == 1 ? 1 :0;

return $exists; 
}
?>
于 2013-07-18T12:36:32.537 回答