Supplied argument is not a valid MySQL result resource

Reply

Join Date: Apr 2009
Posts: 3
Reputation: superyo is an unknown quantity at this point 
Solved Threads: 0
superyo superyo is offline Offline
Newbie Poster

Re: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

 
0
  #1
Apr 18th, 2009
i got this error:
  1. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\db.php on line 13

my php code is this:
  1. <?php
  2. //Database Info
  3. $dbhost="localhost";
  4. $dbuser="root";
  5. $dbpass="something";
  6. $dbname="somedb";
  7. $table="users";
  8. //Connect to Database
  9. $con=mysql_connect($dbhost, $dbuser, $dbpass);
  10. mysql_select_db($dbname, $con);
  11. $user=$_POST['user'];
  12. $pass=$_POST['pass'];
  13. $query = "SELECT * FROM $table WHERE Username=$user AND Password=$pass";//MySQL Query
  14. $result=mysql_query($query, $con);//Line 13 ERROR
  15. $numresults=mysql_num_rows($result);//Check The Number Of Results
  16. if($numresults == 1)
  17. {
  18. $handle=fopen("boxsite.html", "a+");
  19. $contents=fread($handle, filesize("boxsite.html"));
  20. fclose($handle);
  21. echo $contents;
  22. }
  23. else
  24. {
  25. $handle=fopen("LoginFail.html", "r");
  26. $contents=fread($handle, filesize("LoginFail.html"));
  27. fclose($handle);
  28. echo $contents;
  29. }
  30. ?>
i searched numerous times but i can't find what's wrong with my query. plz help me
Last edited by superyo; Apr 18th, 2009 at 2:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 189
Reputation: martin5211 is an unknown quantity at this point 
Solved Threads: 14
martin5211 martin5211 is offline Offline
Junior Poster

Re: Supplied argument is not a valid MySQL result resource

 
0
  #2
Apr 18th, 2009
Enclose strings between quotes:

  1. $query = "SELECT * FROM $table WHERE Username='".$user."' AND Password='".$pass."'";
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 79
Reputation: rudevils is an unknown quantity at this point 
Solved Threads: 9
rudevils rudevils is offline Offline
Junior Poster in Training

Re: Supplied argument is not a valid MySQL result resource

 
0
  #3
Apr 19th, 2009
mmm ithink there's a problem with your query.
try to echo-ing to query like this :
  1. echo $query;
is it read $user and $pass variable ??
if its not read those variable you can change query syntak like this :
  1. $query = "SELECT * FROM $table WHERE Username='$user' AND Password='$pass';
  2.  
If love is blind, why there's a bikini ??

Post your article at Bali Side Notes share your knowledge
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: superyo is an unknown quantity at this point 
Solved Threads: 0
superyo superyo is offline Offline
Newbie Poster

Re: Supplied argument is not a valid MySQL result resource

 
0
  #4
Apr 19th, 2009
Originally Posted by martin5211 View Post
Enclose strings between quotes:

  1. $query = "SELECT * FROM $table WHERE Username='".$user."' AND Password='".$pass."'";
Thank you so much that fixed it. I would never understand where the problem was though. I have never seen a syntax like this. Quotes, double quotes and full stops. I know the problem is gone but could you please explain why it is like this? U see i want to learn the correct way by understanding it and not by heart. Thank u in advance
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 189
Reputation: martin5211 is an unknown quantity at this point 
Solved Threads: 14
martin5211 martin5211 is offline Offline
Junior Poster

Re: Supplied argument is not a valid MySQL result resource

 
0
  #5
Apr 19th, 2009
Strings, values (except numeric) in SQL must be enclosed into quotes. Quotes delimit the string content, avoiding being confused by the interpreter, due to a string can contain spaces and non-alphanumeric characters.

You started the SQL sentence with double quotes , it's important to end it with the same quotes. After the sentence I use dots to concatenate (join) variables results and add another portion of text (like the a single quote to end the string).

If that code is a login script, I suggest to use mysql_real_escape_string() on the SQL query variables to avoid a common vulnerability called MySQL injection. This function will convert the quotes that can be used on username field so the user cannot rewrite the SQL query.

More details:

http://php.net/mysql_real_escape_string
Last edited by martin5211; Apr 19th, 2009 at 11:07 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: superyo is an unknown quantity at this point 
Solved Threads: 0
superyo superyo is offline Offline
Newbie Poster

Re: Supplied argument is not a valid MySQL result resource

 
0
  #6
Apr 19th, 2009
Originally Posted by martin5211 View Post
Strings, values (except numeric) in SQL must be enclosed into quotes. Quotes delimit the string content, avoiding being confused by the interpreter, due to a string can contain spaces and non-alphanumeric characters.

You started the SQL sentence with double quotes , it's important to end it with the same quotes. After the sentence I use dots to concatenate (join) variables results and add another portion of text (like the a single quote to end the string).

If that code is a login script, I suggest to use mysql_real_escape_string() on the SQL query variables to avoid a common vulnerability called MySQL injection. This function will convert the quotes that can be used on username field so the user cannot rewrite the SQL query.

More details:

http://php.net/mysql_real_escape_string
Well if i undestood correctly what u said was that the single quotes(' ') u use come from the SQL syntax and are used for strings and the double quotes(" ") with the dots come from PHP and join variables that we want to display with other text. Now because we write a php script and it contains an SQL query we use them together?
Oh and thanx for the tip!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 189
Reputation: martin5211 is an unknown quantity at this point 
Solved Threads: 14
martin5211 martin5211 is offline Offline
Junior Poster

Re: Supplied argument is not a valid MySQL result resource

 
0
  #7
Apr 19th, 2009
That's correct. There is better methods to show the code in a more readable way e.g. using sprintf() like in PHP.net mysql_real_escape_string() reference, curly braces on variables, doing escaping before SQL query $user=mysql_real_escape_string($_POST['user']); .
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC