...

View Full Version : Warning: mysql_result(): supplied argument is not a valid MySQL result resource



tomfmason
07-08-2006, 11:27 AM
I have am trying(unsuccessfully) to create a login script. I get the following error

heres the code:


Warning: mysql_result(): supplied argument is not a valid MySQL result resource in test_login.php on line 13


<?php
include ('includes/db.php');
array_pop($_POST);
if ( get_magic_quotes_gpc() ) {
$_POST= array_map('stripslashes', $_POST);
}

$username= mysql_real_escape_string(trim($_POST['username']));
$password= mysql_real_escape_string(trim($_POST['password']));

$sql= sprintf("SELECT COUNT(*) AS login_match FROM `users` WHERE `username` = '%s' AND `user_password`= '%s'", $username, $password);
$res= mysql_query($sql);
$login_match= mysql_result($res, 0, 'login_match');

if ( $login_match == 1 ) {
echo "This test worked";
} else {
echo "This test did not work";
// not logged in
}
?>

line 13:


$login_match= mysql_result($res, 0, 'login_match');

I am wondering if anyone can suggest as to why this is happening?

tomfmason
07-08-2006, 12:04 PM
it was a simple error. I changed this line

From:


$res= mysql_query($sql);

To:


$res= mysql_query($sql) or die(mysql_error());

This told me that user_password was not valid..lol. A simple error
I am running into another problem. It is not logining me in. I keep getting the error message


} else {
echo "This test did not work";

Any suggestions as to why this is not loging in?

_Aerospace_Eng_
07-08-2006, 12:10 PM
Try echoing $login_match and see what is getting returned.

tomfmason
07-08-2006, 12:16 PM
it is echoing 0. So this means that there is something wrong with the way that I have $login_match setup.

Could you suggest a better way

_Aerospace_Eng_
07-08-2006, 12:21 PM
What row are the username and password stored on? Have you read the manual on mysql_result()? http://us2.php.net/mysql_result

tomfmason
07-08-2006, 12:37 PM
username is on row 0 and passowrd is on row 1

tomfmason
07-08-2006, 12:56 PM
it was a simple error. Thanks for your help. I had to use mysql_num_rows instead of mysql_result..lol

GJay
07-08-2006, 01:03 PM
the number of rows by
SELECT COUNT(*) will always be 1, so I'm not convinced you will have fixed it.
The query would need to be something like:
SELECT username FROM...WHERE...
and that will return 1 row when there is a valid match.

thunderhoster
07-08-2006, 01:22 PM
Try something like this (untested)

<?php
include ('includes/db.php');
array_pop($_POST);
if ( get_magic_quotes_gpc() ) {
$_POST= array_map('stripslashes', $_POST);
}

$username= mysql_real_escape_string(trim($_POST['username']));
$password= mysql_real_escape_string(trim($_POST['password']));

$sql = "SELECT * FROM `users` WHERE username LIKE ".$username." AND user_password = ".$password."";
$query = mysql_query($sql);
$res = mysql_num_rows($query);

if ( $res == 1 ) {
echo "This test worked";
} else {
echo "This test did not work";
// not logged in
}
?>


it was a simple error. Thanks for your help. I had to use mysql_num_rows instead of mysql_result..lol Sorry, i didn't refresh my page and consequently didn't see your post.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum