Page 1 of 1

Warning: mysql_result(): supplied argument is not a valid

Posted: Thu Dec 01, 2011 10:12 pm
by D13G0
Hey

I'm following the user account tutorials and I've been hit with this problem. I've read all the threads on this but nothing seems to help me. I'm not sure if it's something wrong with the coding or with my database.

This is the error I get when I click "register" in register.php without filling in the form. I noticed when he clicked it without filling it out, he got no error:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /hermes/bosweb26b/b2305/ipg.truecoloursdaycareca/qstdy/user.inc.php on line 7
So this is my code for user.inc.php:
<?php

// Checks 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 is 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}') ");
 
}
		
?>
I'm getting the error for:
return (mysql_result($total, 0) == '1') ? true : false;
What can I do to fix this?

Thank you.

Re: Warning: mysql_result(): supplied argument is not a vali

Posted: Thu Dec 01, 2011 10:44 pm
by D13G0
And WHEN I do fill out the register form and click "Register", I get:

Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb26b/b2305/ipg.truecoloursdaycareca/qstdy/user.inc.php:7) in /hermes/bosweb26b/b2305/ipg.truecoloursdaycareca/qstdy/register.php on line 31

Register.php:
<?php

include('init.inc.php');

$errors = array();

if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){
	if (empty($_POST['username'])){
		$errors[] = 'The username cannot be empty.';
	}
	
	if (empty($_POST['password']) || empty($_POST['repeat_password'])){
		$errors[] = 'The password cannot be empty.';
		
	}
	
	if ($_POST['password'] !== $_POST['repeat_password']){
		$errors[] = 'Password verification failed.';
	}
	
	if (user_exists($_POST['username'])){
		$errors[] = 'The username you entered is already taken.';
		
	}
	
	if (empty($errors)){
		add_user($_POST['username'], $_POST['password']);
		
		$_SESSION['username'] = htmlentities($_POST['username']);
		
		header('location: test.php');
		die();
	}
	
}



?>

Re: Warning: mysql_result(): supplied argument is not a vali

Posted: Fri Dec 02, 2011 12:49 am
by jacek
Try adding a
echo mysql_error();
somewhere after the query is run, that usually shows up the problem for this kind of error.

Have a go at the first, it's possible that the header error is actually caused by the first error being shown.

Re: Warning: mysql_result(): supplied argument is not a vali

Posted: Fri Dec 02, 2011 2:49 am
by D13G0
Ive never used that before. Can I insert it in the 8th line after the error?

And what do I do next? Refresh the register.php page and it should come up?

Thanks.

Re: Warning: mysql_result(): supplied argument is not a vali

Posted: Sat Dec 03, 2011 7:01 pm
by jacek
You can put it on line 32 of register.php, if you put it after the return you wont see it's output.