View Full Version : not a valid MySQL result resource [was: I'm new and need quick help]

11-08-2006, 09:59 PM
Hiya, I was wondering if anybody can help.

I have a website which i have a login script for however I want to users to be able to have an account balance. I have set up a field for balance in the users table of my database however I am having problems fetching the correct row for each user.

This is my code:


$mysqlLink = mysql_connect( 'mysql2.freehostia.com' , 'My Username', 'My Password' );

mysql_select_db( 'joesha1_members' , $mysqlLink );

$result = mysql_query("SELECT * FROM `users` WHERE `username`= ".$_SESSION['username']." AND `password`=".$_SESSION['password']." ");

while( $row = mysql_fetch_array($result) )
echo $row[ 'username' ] . $row[ 'balance' ];

mysql_close( $mysqlLink );


It keeps on displaying this error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/joesha1.freehostia.com/account.php on line 19

Thanks and hope i made any sence, Joe

11-08-2006, 11:17 PM
look in the PHP forum for your specific error message. I'll move your thread there as PHP is the cause of the problem and not the query per se.

11-09-2006, 05:57 AM
Something is probably wrong with your query. I would change around the script just a little to try and diagnose the problem. Take this statement:

$result = mysql_query("SELECT * FROM `users`
WHERE `username`= ".$_SESSION['username'].
" AND `password`=".$_SESSION['password']." ");
and replace it with this:

$query = "SELECT * FROM `users` WHERE `username`= ".$_SESSION['username'].
" AND `password`=".$_SESSION['password'];
echo "query = ". $query . "<br>";
$result = mysql_query($query) or die(mysql_error);
This will hopefully show you if something is wrong in the way the query is setup and will show you the actual error.

11-09-2006, 07:32 AM
if your username and password are strings, then they need to be in quotes inside the query.

$query="SELECT blah FROM table WHERE username='".mysql_real_escape_string($_SESSION['username'])."'";

11-09-2006, 08:14 AM
As a good practice it is good to print the query and see if it is what you expected (errors may occur when appending a variable contents which is actually empty).

Therefore, I recommend:

$query = "SELECT * FROM `users` WHERE username = `".$_SESSION['username']."` AND password =`".$_SESSION['password']."` ";
echo $query; //check if it's the expected query
$result = mysql_query($query);

P.S.: there's no need to quote the name of the fields (username, password), but the value you check it against unless it's integer

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum