Forum Moderators: open
$query = "SELECT * FROM users WHERE name='$name'";
I get the following error
Unknown column '$name' in 'field list'
$name being the variable I'm checking to see if exists
I only get that error when $name isn't in the table and thats what I need to check for so if anybody has any insight into whats wrong with my query above please help.
There are two ways around it. Either you can modify your sql to
$query = "SELECT count(*) FROM users WHERE name='$name';";
which will guarantee you a result where you can just check that it's greater than 0 or you can use the
mysql_num_rows() function to check that you actually have a row before you fetch the first row e.g.
// does user exist?
$name = mysql_real_escape_string($name);
$query = "SELECT * FROM users WHERE name='$name';";
$res = mysql_query($query);
if (mysql_num_rows($res) > 0) {
// yes, pull in the user details
} else
// no, user doesn't exist
}