Warning: mysql_num_rows() expects parameter 1 to be resource

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NewVideo
Forum Newbie
Posts: 6
Joined: Sun Jan 16, 2011 10:48 pm

Warning: mysql_num_rows() expects parameter 1 to be resource

Post by NewVideo »

Error Obtained: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/<BEEP>/public_html/search/search.php on line 54

Here...

Code: Select all

 //echo outconstruct
  $constructx = "SELECT * FROM searchengine WHERE $construct";
  
  $construct = "SELECT * FROM searchengine WHERE $construct LIMIT $s,$e";
  $run = mysql_query($constructx);
  
  $foundnum = mysql_num_rows($run);
  or die(mysql_error())

  $run_two = mysql_query("$construct");
  
  if ($foundnum==0)
   echo "No results found for <b>$search</b>";
  else
  {
   echo "<table bgcolor='#0000FF' width='100%' height='1px'><br /></table><table bgcolor='#f0f7f9' width='100%' height='10px'><tr><td><div align='right'>Showing 1-10 of <b>$foundnum</b> results found for <b>$search.</b></div></td></tr></table><p>";
   
   while ($runrows = mysql_fetch_assoc($run_two))
   {
    //get data
   $title = $runrows['title'];
   $desc = $runrows['description'];
   $url = $runrows['url'];
Problem Line is $foundnum = mysql_num_rows($run);
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Warning: mysql_num_rows() expects parameter 1 to be reso

Post by califdon »

This message is telling you that mysql_num_rows() must have a pointer to a valid result set, but it is being given a boolean value (true or false). This is caused by your query not producing a valid result set, several lines above. The proper way to detect query errors like this during the testing and debugging phase is to temporarily amend your query execution line like this:

Code: Select all

$run = mysql_query($constructx) or die(mysql_error());
Now if your query fails, the MySQL error message will be sent to the browser and the script will terminate. Then you will see what the problem is. Once everything is working correctly, you should remove the "or die(...)" part, so that it won't accidentally be triggered once you launch your script publicly.
NewVideo
Forum Newbie
Posts: 6
Joined: Sun Jan 16, 2011 10:48 pm

Re: Warning: mysql_num_rows() expects parameter 1 to be reso

Post by NewVideo »

that gave me a

Parse error: syntax error, unexpected T_LOGICAL_OR in /home/newtube/public_html/search/search.php on line 52
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Warning: mysql_num_rows() expects parameter 1 to be reso

Post by Jonah Bron »

I assume line 52 is the line califdon posted? It shouldn't give that error... please paste the exact code you're using as it is now (with the change provided by califdon).
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Warning: mysql_num_rows() expects parameter 1 to be reso

Post by califdon »

There must be more to your script. Where does $construct come from? You are using the same variable as a WHERE clause criterion and as the name of the SQL statement. That makes no sense. That last error probably comes from an invalid line that begins with "or". That must be a typo. I'm afraid none of your script makes any sense to me.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Warning: mysql_num_rows() expects parameter 1 to be reso

Post by social_experiment »

Code: Select all

<?php
$foundnum = mysql_num_rows($run);
  or die(mysql_error())

// should be
$foundnum = mysql_num_rows($run) or die(mysql_error());  
?>
Your SELECT query also looks wrong, SELECT * FROM searchengine WHERE $construct doesn't make sense. (Unless $construct contains a string like 'id = 5')
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
NewVideo
Forum Newbie
Posts: 6
Joined: Sun Jan 16, 2011 10:48 pm

Re: Warning: mysql_num_rows() expects parameter 1 to be reso

Post by NewVideo »

hmm its a "Search engine script"

also
heres the whole search.php page...

Code: Select all

<?php


//get data
$button = $_GET['submit'];
$search = $_GET['search'];


$s = $_GET['s'];
if (!$s)
$s = 0;


$e = 10; // Just change to how many results you want per page


$next = $s + $e;
$prev = $s - $e;




 if (strlen($search)<=2)
  echo "Current Minimal Characters 2";
 else
 {
  echo "<br /><table><tr><td><img src='searchenginelogo' /></td><td><form action='search.php' method='GET'><input type='text' onclick=value='' size='50' name='search' value='$search'> <input type='submit' name='submit' value='Search'></form></td></tr></table>";
  
  //connect to database
  mysql_connect("localhost","newtube_admin","BEEP!");
  mysql_select_db("search");
   
   //explode out search term
   $search_exploded = explode(" ",$search);
   
   foreach($search_exploded as $search_each)
   {
   
        //construct query
    $x++;
    if ($x==1)
     $construct .= "keywords LIKE '%$search_each%'";
    else
     $construct .= " OR keywords LIKE '%$search_each%'";
   
   }
   
  //echo outconstruct
  $constructx = "SELECT * FROM searchengine WHERE $construct";
  
  $construct = "SELECT * FROM searchengine WHERE $construct LIMIT $s,$e";
  $run = mysql_query($constructx); or die(mysql_error());          <-----------
  
  $foundnum = mysql_num_rows($run);              <---------------------
  

  $run_two = mysql_query("$construct");
  
  if ($foundnum==0)
   echo "No results found for <b>$search</b>";
  else
  {
   echo "<table bgcolor='#0000FF' width='100%' height='1px'><br /></table><table bgcolor='#f0f7f9' width='100%' height='10px'><tr><td><div align='right'>Showing 1-10 of <b>$foundnum</b> results found for <b>$search.</b></div></td></tr></table><p>";
   
   while ($runrows = mysql_fetch_assoc($run_two))
   {
    //get data
   $title = $runrows['title'];
   $desc = $runrows['description'];
   $url = $runrows['url'];
   
   echo "<table width='300px'>
   <h4><a href='http://$url'><b>$title</b></a><br />
   $desc<br>
   <font color='00CC00'>$url</font></table></h4>
   ";
   }
?>

<table width='100%'>
<tr>
<td>
<div align="center">

<?php
if (!$s<=0)
 echo "<a href='search.php?search=$search&s=$prev'>Prev</a>";

$i =1; 
for ($x=0;$x<$foundnum;$x=$x+$e)
{


 echo " <a href='search.php?search=$search&s=$x'>$i</a> ";


$i++;


}

if ($s<$foundnum-$e)
  echo "<a href='search.php?search=$search&s=$next'>Next</a>";

	}
}  


?>
</div>
</td>
</tr>
</table>

I put a arrow on LINES 52, and 54
NewVideo
Forum Newbie
Posts: 6
Joined: Sun Jan 16, 2011 10:48 pm

Re: Warning: mysql_num_rows() expects parameter 1 to be reso

Post by NewVideo »

if anyone needs any other info, just ask...
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Warning: mysql_num_rows() expects parameter 1 to be reso

Post by Jonah Bron »

You placed a semicolon that's not supposed to be there.

Code: Select all

$run = mysql_query($constructx); or die(mysql_error());
Needs to be exactly as califdon gave.

Code: Select all

$run = mysql_query($constructx) or die(mysql_error());
NewVideo
Forum Newbie
Posts: 6
Joined: Sun Jan 16, 2011 10:48 pm

Re: Warning: mysql_num_rows() expects parameter 1 to be reso

Post by NewVideo »

I didn't place it there; or I THOUGHT i didn't.

Anyway;

Doing that gave me the explaination of:
No database selected


The whole code can be viewed in one of my previous posts.

If you need to see it.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Re: Warning: mysql_num_rows() expects parameter 1 to be reso

Post by d3ad1ysp0rk »

http://php.net/mysql_select_db

That is a necessary part of executing a query.

Connect.. Select DB.. Query.. Fetch Result.
NewVideo
Forum Newbie
Posts: 6
Joined: Sun Jan 16, 2011 10:48 pm

Re: Warning: mysql_num_rows() expects parameter 1 to be reso

Post by NewVideo »

What do you mean?

What code do I need to add, fix, or remove.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Warning: mysql_num_rows() expects parameter 1 to be reso

Post by Jonah Bron »

You need to use mysql_select_db() to select whatever database you're using. Probably right after mysql_connect(). You see, a MySQL server is partitioned into separate databases, each holding it's own tables. You must tell your MySQL connection which database you want to communicate with.
Post Reply