Page 1 of 1

Register and Login [Part 02]

Posted: Sun Mar 04, 2012 7:10 pm
by Owinzin
Well so I am getting this error upon submitting my form:
Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\devshare\core\func\user.func.php on line 35
And this is the code it isn't liking:
function userExists($username) {
	$username = mysql_real_escape_string($username);
	
	$total = mysql_query("SELECT COUNT('id') FROM users WHERE username =$username");
	
	return (mysql_result($total, 0) == '1') ? true : false;
}
Please help, thank you.

Re: Register and Login [Part 02]

Posted: Sun Mar 04, 2012 7:25 pm
by Temor
your using single-quotation marks ( ' ) where you should be using backticks ( ` )

if you add
echo mysql_error();

under the query that is failing it will tell you what's wrong.

Re: Register and Login [Part 02]

Posted: Sun Mar 04, 2012 8:45 pm
by Owinzin
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
I changed it to backticks.

Re: Register and Login [Part 02]

Posted: Mon Mar 05, 2012 10:44 pm
by jacek
You also want quotes around $username at the end of the SQL.

Re: Register and Login [Part 02]

Posted: Wed Mar 07, 2012 5:22 pm
by Owinzin
I don't see why I do? I have used a variable in MYSQL without quotes and it has worked fine. But I will try anyway.

Re: Register and Login [Part 02]

Posted: Thu Mar 08, 2012 1:15 am
by jacek
Owinzin wrote:I don't see why I do? I have used a variable in MYSQL without quotes and it has worked fine. But I will try anyway.
It's not to do with the fact that it's a variable.

When you put a variable in a string like that it just replaced it with the variables value. If you variable contains a sting it needs quotes or mysql will not know what to do with it.

Re: Register and Login [Part 02]

Posted: Sun Mar 11, 2012 7:04 pm
by Owinzin
Now it won't send to the database.

Re: Register and Login [Part 02]

Posted: Mon Mar 12, 2012 8:14 pm
by jacek
Owinzin wrote:Now it won't send to the database.
Post your code.

Re: Register and Login [Part 02]

Posted: Wed Mar 14, 2012 4:23 pm
by Owinzin
	
function userRegister($username, $password){
$username = mysql_real_escape_string(htmlentities($username));
	$password = sha1($password);
	
	mysql_query("INSERT INTO users (username, password) VALUES ($username, $password)");

}

Re: Register and Login [Part 02]

Posted: Wed Mar 14, 2012 4:49 pm
by Temor
you need single quotation marks around your variables, and also backticks around your column names.
function userRegister($username, $password){
        $username = mysql_real_escape_string(htmlentities($username));
        $password  = sha1($password);
       
        mysql_query("INSERT INTO users (`username`, `password`) VALUES ('$username', '$password')");
 
}