943,522 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1689
  • PHP RSS
Apr 18th, 2009
0

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

Expand Post »
i got this error:
PHP Syntax (Toggle Plain Text)
  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:
php Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
superyo is offline Offline
3 posts
since Apr 2009
Apr 18th, 2009
0

Re: Supplied argument is not a valid MySQL result resource

Enclose strings between quotes:

php Syntax (Toggle Plain Text)
  1. $query = "SELECT * FROM $table WHERE Username='".$user."' AND Password='".$pass."'";
Reputation Points: 52
Solved Threads: 23
Posting Whiz in Training
martin5211 is offline Offline
271 posts
since Aug 2007
Apr 19th, 2009
0

Re: Supplied argument is not a valid MySQL result resource

mmm ithink there's a problem with your query.
try to echo-ing to query like this :
PHP Syntax (Toggle Plain Text)
  1. echo $query;
is it read $user and $pass variable ??
if its not read those variable you can change query syntak like this :
PHP Syntax (Toggle Plain Text)
  1. $query = "SELECT * FROM $table WHERE Username='$user' AND Password='$pass';
  2.  
Reputation Points: 10
Solved Threads: 10
Junior Poster in Training
rudevils is offline Offline
80 posts
since Jan 2008
Apr 19th, 2009
0

Re: Supplied argument is not a valid MySQL result resource

Click to Expand / Collapse  Quote originally posted by martin5211 ...
Enclose strings between quotes:

php Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
superyo is offline Offline
3 posts
since Apr 2009
Apr 19th, 2009
0

Re: Supplied argument is not a valid MySQL result resource

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.
Reputation Points: 52
Solved Threads: 23
Posting Whiz in Training
martin5211 is offline Offline
271 posts
since Aug 2007
Apr 19th, 2009
0

Re: Supplied argument is not a valid MySQL result resource

Click to Expand / Collapse  Quote originally posted by martin5211 ...
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
superyo is offline Offline
3 posts
since Apr 2009
Apr 19th, 2009
0

Re: Supplied argument is not a valid MySQL result resource

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']); .
Reputation Points: 52
Solved Threads: 23
Posting Whiz in Training
martin5211 is offline Offline
271 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: edit PDF with PHP
Next Thread in PHP Forum Timeline: Return Car Make from MySQL database & have the make hyperlinked.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC