Login System: MySQL_Result error

Post here is you are having problems with any of the tutorials.
Post Reply
Snakybo
Posts: 9
Joined: Thu Jan 05, 2012 3:35 pm

Login System: MySQL_Result error

Post by Snakybo »

From your tutorial Register and Login (User account system) (http://betterphp.co.uk/playlist.html?pi ... A12314F07E) I got the following error:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/a6713569/public_html/TGz/Content/Features/UAS/core/inc/user.inc.php on line 19
Thats what I get if I try to login, but if I try to register I get this:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/a6713569/public_html/TGz/Content/Features/UAS/core/inc/user.inc.php on line 9
It's both the same code so the same error I cannot find >.<

I compared your code with mine like 10 times but I cannot seem to find the error. Any help?

Here's my code:
<?php

// Check if the username already exists.
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;
}

// Check if the username and password are valid.
function valid_credentials($user, $pass){
	$user = mysql_real_escape_string(htmlentities($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;
}

// Add new 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}')");
}

?>
SkillBuzz
Posts: 13
Joined: Wed Apr 04, 2012 7:50 am

Re: Login System: MySQL_Result error

Post by SkillBuzz »

Well, for one, you are using the wrong quote marks.

rather than:
$total = mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'user_name' = '{$user}'");
it should be:
$total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_name`='{$user}'");
It's ` not ' .
Snakybo
Posts: 9
Joined: Thu Jan 05, 2012 3:35 pm

Re: Login System: MySQL_Result error

Post by Snakybo »

Oh thanks, but how do I create those? Don't want to Copy them each time I need it
SkillBuzz
Posts: 13
Joined: Wed Apr 04, 2012 7:50 am

Re: Login System: MySQL_Result error

Post by SkillBuzz »

Depends on your keyboard..


What country?
Snakybo
Posts: 9
Joined: Thu Jan 05, 2012 3:35 pm

Re: Login System: MySQL_Result error

Post by Snakybo »

I'm from the Netherlands, my keyboard is United States (International)
SkillBuzz
Posts: 13
Joined: Wed Apr 04, 2012 7:50 am

Re: Login System: MySQL_Result error

Post by SkillBuzz »

the ` you want should be next to the number 1, above the tab.
Snakybo
Posts: 9
Joined: Thu Jan 05, 2012 3:35 pm

Re: Login System: MySQL_Result error

Post by Snakybo »

Oh thanks, but now problem 2, this comes from the email activation part:
Parse error: syntax error, unexpected ';' in /home/a6713569/public_html/TGz/Content/Features/UAS/core/inc/user.inc.php on line 35
<?php

// Check if the username already exists.
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;
}

// Check if the username and password are valid.
function valid_credentials($user, $pass){
	$user = mysql_real_escape_string($user);
	$pass = mysql_real_escape_string($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;
}

// Check if the account is activated.
function is_active($user){
	$user = mysql_real_escape_string($user);
	
	$sql = "SELECT
				COUNT(`user_activations`.`user_id`)
			FROM `users`
			INNER JOIN `user_activations`
			ON `users`.`user_id` = `user_activations`.`user_id`
			WHERE `users`.`user_name` = '{$user}'";
			
	$result = mysql_query($sql);
	
	return (mysql_result($result, 0) == '0' ? true : false;
}

function activate_account($aid){
	$aid = mysql_real_escape_string($aid);
	
	mysql_query("DELETE FROM `user_activations` WHERE `activation_code` = '{$aid}'");
}

// Add new user to the database.
function add_user($user, $email, $pass){
	$user	 = mysql_real_escape_string(htmlentities($user));
	$email	 = mysql_real_escape_string($email);
	$pass	 = sha1($pass);
	
	$charset = array_flip(array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9)));
	$aid = implode('', array_rand($charset, 20));
	
	$body = <<<EMAIL
	
	Hi {$user}!
	
	Thanks for registering on MineCraftNeo, however bevore you can login you need to activate your account.
	To activate your account simply click on the link below or copy and paste it.
	
	http://mcneo.co.cc/TGz/Content/Features ... ?aid={$aid}
	
EMAIL;

	mail('$email', 'Activate your MineCraftNeo account!', '$body', 'From: activate@mcneo.co.cc');
	
	mysql_query("INSERT INTO `users` (`user_name`, `user_password`, `user_email`) VALUES ('{$user}', '{$pass}', '{$email}')");
	
	$user_id = mysql_insert_id();
	
	mysql_query("INSERT INTO `user_activations` (`user_id`, `activation_code`) VALUES ({$user_id}, '')");
}

?>
So am I just being blind here? That same code is being used at line 9 with just one difference, and it works there
SkillBuzz
Posts: 13
Joined: Wed Apr 04, 2012 7:50 am

Re: Login System: MySQL_Result error

Post by SkillBuzz »

it even tells you where the error is..

i'll give you 1 hint: )
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Login System: MySQL_Result error

Post by jacek »

I'll give a second hint, "you missed a " :D
Image
Post Reply