Page 1 of 1

Register and Login

Posted: Thu Jun 21, 2012 2:36 pm
by warren28
Hi I have been following your tutorials for a while now and I am stuck with this one!

I keep getting the error message: Warning: mysql_result() expects parameter 1 to be resource, boolean given in /customers/2/b/e/warrenamphlett.co.uk/httpd.www/core/inc/user.inc.php on line 9

I have checked everything that I know to check, but now I'm stumped because I followed your ever step to a T...
here is my code:
<?php

// Check if the given username exists in the database.
function user_exists($user){
	$user = mysql_real_escape_string($user);

	$total = mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'user_name' = '{$user}'");	

	return (mysql_result($total, 0) == '1') ? true : false;
}

// Checks if the given username and password combination is valid.
function valid_credentials($user, $pass){
	$user = mysql_real_escape_string($user);
	$pass = sha1($pass);
	
	$total = mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'user_name' = '{$user}' AND 'user_password' = '{$pass}'");	

	return (mysql_result($total, 0) == '1') ? true : false;

}

// Adds a user to the database
function add_user($user, $pass){
	$user = mysql_real_escape_string(htmlentities($user));
	$pass = sha1($pass);
	
	mysql_query("INSERT INTO 'users' ('user_name', 'user_password') VALUES ('{$user}', '{$pass}')");
	
}

?>
Please please help...

Re: Register and Login

Posted: Thu Jun 21, 2012 4:01 pm
by Temor
That error usually means that the SQL query failed. Usually to a typo in the SQL statement.
Adding
echo mysql_error();
under the query that is failing usually tells you whats wrong.

Re: Register and Login

Posted: Thu Jun 21, 2012 5:05 pm
by warren28
Ok I have checked for typos and I can't find any at all.

don't know if this helps, but when I attempt to add the user, I get this error, it doesn't redirect and nothing gets added to the database: Warning: mysql_result() expects parameter 1 to be resource, boolean given in /customers/2/b/e/warrenamphlett.co.uk/httpd.www/users/core/inc/user.inc.php on line 9 Warning: Cannot modify header information - headers already sent by (output started at /customers/2/b/e/warrenamphlett.co.uk/httpd.www/users/core/inc/user.inc.php:9) in /customers/2/b/e/warrenamphlett.co.uk/httpd.www/users/register.php on line 26

I did as you suggested and added the "echo mysql_error();" but it just showed exactly the same error messages.

Re: Register and Login

Posted: Thu Jun 21, 2012 6:24 pm
by Temor
Well, it should tell you something. Did you echo it under the result line?
The problem is that you're using apostrophes ( ' ) instead of backticks ( ` ) around your table names and column names. Apostrophes ( or single quotes ) should be used around variables containing a string.