1

I have recently been following some tutorials about making a login and registration system. I have followed the tutor extremely closely all the way through and we have the exact same code, although I am getting a "Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\idea\core\functions\users.php on line 5" every time.

Line 5 refers to return (mysql_result($query, 0) == 1) ? true : false;, which is the return statement on a MySQL query. This is stopping me from progressing and I have gotten this far, so choosing a different tutorial is not really an option.

Sorry, thought I should add the actual query: $query = mysql_query("SELECT COUNT(user_id) FROM users WHERE username = '$username'"); There were previously backticks around user_id, users and username, although I couldn't add those in.

DingusKhan
  • 135
  • 1
  • 15
  • 2
    you have an invalid query `$query` which returns `false` , `return (mysql_result(false, 0) == 1)` also returns the same error – user2092317 Nov 01 '13 at 23:08
  • 3
    Have you checked the right-hand side-bar? – jeroen Nov 01 '13 at 23:08
  • It's a sign that the `mysql_query()` call went wrong. It's bad style not to catch errors after the query call using `mysql_error()`. Also, the mysql_* library is really outdated unfortunately, it would be best to get started with mysqli or PDO (which has a slightly steeper learning curve but is all the rage these days) – Pekka Nov 01 '13 at 23:08
  • When you get an error, [search for it](http://stackoverflow.com/search?q=mysql_result%28%29+expects+parameter+1+to+be+resource%2C+boolean+given)! – halfer Nov 01 '13 at 23:12

2 Answers2

2

As the php manual on mysql_query tells you:

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

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

So most likely your query doesn't work out and mysql_query returns false.

Check that first (if ($query === false) {echo mysql_error();}), and only proceed if the query ran fine.

Community
  • 1
  • 1
Johannes H.
  • 5,822
  • 1
  • 19
  • 39
  • Thank you all for the help. I accidentally named a column in my database "id" instead of "user_id" and forgot to debug before running the code properly. – DingusKhan Nov 01 '13 at 23:26
  • You should still include proper error handling in your code. If your database connection fails for some reason (which is something that can always happen, no matter how well written your code is) for example, you are likely to get the same errors again. – Johannes H. Nov 01 '13 at 23:43
1

First off, use mysqli, not mysql. The latter is depreciated

Second, the syntax of mysqli_query is that you pass the connection pointer first, then your query. So you need to call mysqli_connect first

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
$result = mysqli_query($link, 'SELECT * FROM table');
Machavity
  • 29,816
  • 26
  • 86
  • 96
  • 1
    His syntax is (despite the deprecated interface - if he's using php5.5 or later) fine. If the previous query doesn't yield any errors, his code works, there is nothing wrong with the order of arguments he passes. – Johannes H. Nov 01 '13 at 23:13