Jump to content


Photo

Mysql_result(): supplied argument is not a valid MySQL result


  • Please log in to reply
3 replies to this topic

#1 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 08 July 2006 - 08:28 AM

I have am trying(unsuccessfully) to create a login script. As suggested, I tried it with magic_quotes. I get the following error.

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

and here is the code

<?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?

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 08 July 2006 - 09:57 AM

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?

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 08 July 2006 - 10:19 AM

I tried echoing $login_match and it is returning 0.

here is what I have for login match

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

I am lost ???

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 08 July 2006 - 11:09 AM

LOL. It was rather simple. I needed to use mysql_num_rows instead of mysql_result. Now I need to work on the sessions.

*The Fix:
<?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 `password`= '%s'", $username, $password); 
$res= mysql_query($sql) or die(mysql_error()); 
$login_match= mysql_num_rows($res) or die(mysql_error()); 

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

I hope this helps someone else in the future

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users