Page 1 of 1

Warning: mysql_num_rows() expects parameter 1 to be resource

Posted: Sun Jan 16, 2011 10:56 pm
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);

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

Posted: Sun Jan 16, 2011 11:07 pm
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.

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

Posted: Sun Jan 16, 2011 11:15 pm
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

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

Posted: Sun Jan 16, 2011 11:42 pm
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).

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

Posted: Mon Jan 17, 2011 12:04 am
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.

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

Posted: Mon Jan 17, 2011 12:09 am
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')

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

Posted: Mon Jan 17, 2011 1:09 am
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

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

Posted: Mon Jan 17, 2011 1:27 am
by NewVideo
if anyone needs any other info, just ask...

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

Posted: Mon Jan 17, 2011 11:29 am
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());

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

Posted: Mon Jan 17, 2011 1:13 pm
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.

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

Posted: Mon Jan 17, 2011 1:45 pm
by d3ad1ysp0rk
http://php.net/mysql_select_db

That is a necessary part of executing a query.

Connect.. Select DB.. Query.. Fetch Result.

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

Posted: Mon Jan 17, 2011 1:58 pm
by NewVideo
What do you mean?

What code do I need to add, fix, or remove.

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

Posted: Mon Jan 17, 2011 5:00 pm
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.