By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,316 Members | 2,132 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,316 IT Pros & Developers. It's quick & easy.

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

P: 27
Hi! I'm completely new to all dev and trying to put a website together using MySQLI and PHP; was running fine until I try to gather the DB content with the following script:

Expand|Select|Wrap|Line Numbers
  1. <?
  2. $username="xxxxxx";
  3. $password="xxxxxx";
  4. $database="maxalien_davidloran";
  5.  
  6. mysql_connect(localhost,$username,$password);
  7. @mysql_select_db($database) or die( "Unable to select database");
  8. $query="SELECT * FROM contacts";
  9.  
  10. $result=mysql_query($query);
  11.  
  12. $num=mysql_num_rows($result);
  13.  
  14.  
  15. mysql_close();
  16.  
  17. echo "<b><center>Database Output</center></b><br><br>";
  18.  
  19. $i=0;
  20. while ($i < $num) {
  21.  
  22. $username=mysql_result($result,$i,"username");
  23. $password=mysql_result($result,$i,"password");
  24. $email=mysql_result($result,$i,"email");
  25.  
  26. echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";
  27.  
  28. $i++;
  29. }
  30.  
  31. ?>
  32.  
getting the following error mesg when running it:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/maxalien/public_html/davidloran/test/userCake/dbresult.php on line 12
Database Output

Let me know if you need more info and thx for your help
Jan 2 '10 #1

✓ answered by code green

Something is failing here
Expand|Select|Wrap|Line Numbers
  1. mysql_connect(localhost,$username,$password); 
  2. @mysql_select_db($database) or die( "Unable to select database"); 
  3. $query="SELECT * FROM contacts"; 
  4.  
  5. $result=mysql_query($query); 
  6.  
  7. $num=mysql_num_rows($result); 
  8.  
You can help yourself by removing the error supression '@' and
testing the returns using if() and mysql_error().

Also, further down you are closing the database connection then continuing to use it

Share this Question
Share on Google+
2 Replies


P: 27
is anyone able to help please?
Jan 3 '10 #2

code green
Expert 100+
P: 1,726
Something is failing here
Expand|Select|Wrap|Line Numbers
  1. mysql_connect(localhost,$username,$password); 
  2. @mysql_select_db($database) or die( "Unable to select database"); 
  3. $query="SELECT * FROM contacts"; 
  4.  
  5. $result=mysql_query($query); 
  6.  
  7. $num=mysql_num_rows($result); 
  8.  
You can help yourself by removing the error supression '@' and
testing the returns using if() and mysql_error().

Also, further down you are closing the database connection then continuing to use it
Jan 4 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.