-1

Creating a login system for something and am getting:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in .../func/user.func.php on line 21

Here is my code:

function user_register($email, $name, $password) {

}

function user_exists($email) {
  $email = mysql_real_escape_string($email);
  $query = mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'email' ='$email'");

  //this is line 21:
  return (mysql_result($query, 0) == 1) ? true : false;
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
user1043816
  • 139
  • 1
  • 4
  • 14

4 Answers4

1

Your SQL is full of syntax errors, single quotes are used to quote string literals, backticks (or double quotes in standard SQL) are used for identifiers. Try this:

$query = mysql_query("SELECT COUNT(user_id) FROM users WHERE email = '$email'");

You don't need to quote any of those identifiers so don't bother.

From the fine manual:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

You probably want to add some error checking after you've fixed your SQL syntax errors.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
1

There is an error in your query. If you want to figure out what the error is, output it with mysql_error()

jli
  • 6,523
  • 2
  • 29
  • 37
0

Your query has syntax errors. You're using single quotes on field and table names. That changes from them being field/table names to being ordinary strings. So your query boils down to count(some string) from someotherstring where yetanotherstring.

If your query call had been constructed something like this:

$result = mysql_query(...) or die(mysql_error());

you'd have been informed of the syntax error. As it stands now, your code assumes the query succeeds, which is a very bad thing to do. There's precisely ONE way for a query to succeed, and far too many ways for it to fail.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

Your code has no error handling, so if anything goes wrong, it continues on blissfully unaware trying to make soup out of stones. Take a look at the examples in the manual, they all check for errors in the query before they try to analyze the result.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278