Register and Login [Part 02]

Post here is you are having problems with any of the tutorials.
Post Reply
Owinzin
Posts: 5
Joined: Sun Mar 04, 2012 7:07 pm

Register and Login [Part 02]

Post 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.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Register and Login [Part 02]

Post 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.
Owinzin
Posts: 5
Joined: Sun Mar 04, 2012 7:07 pm

Re: Register and Login [Part 02]

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Register and Login [Part 02]

Post by jacek »

You also want quotes around $username at the end of the SQL.
Image
Owinzin
Posts: 5
Joined: Sun Mar 04, 2012 7:07 pm

Re: Register and Login [Part 02]

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Register and Login [Part 02]

Post 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.
Image
Owinzin
Posts: 5
Joined: Sun Mar 04, 2012 7:07 pm

Re: Register and Login [Part 02]

Post by Owinzin »

Now it won't send to the database.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Register and Login [Part 02]

Post by jacek »

Owinzin wrote:Now it won't send to the database.
Post your code.
Image
Owinzin
Posts: 5
Joined: Sun Mar 04, 2012 7:07 pm

Re: Register and Login [Part 02]

Post 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)");

}
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Register and Login [Part 02]

Post 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')");
 
}
Post Reply