PHP and Mysql search engine

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
rodeea
Forum Newbie
Posts: 1
Joined: Sun Jun 21, 2015 12:52 pm

PHP and Mysql search engine

Post by rodeea »

Hello , I have a big problem with this error: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp...

I think it is something to do with the sintax. Please anyone can help me with this?

My code looks like this:

Code: Select all

<?php 
require('connect.php');
$button = $_GET [ 'submit' ];
$search = $_GET [ 'search' ];
if( !$button )
echo "you didn't submit a keyword"; 
else { if( strlen( $search ) <= 1 )
echo "Search term too short";
else { echo "You searched for <b> $search </b> <hr size='1' > </ br > "; 

$search_exploded = explode ( " ", $search );
$x = 0;
foreach( $search_exploded as $search_each ) 
{
$x++; $construct = "";
if( $x == 1 ) $construct .="keywords LIKE '%$search_each%'";
else $construct .="AND keywords LIKE '%$search_each%'";
} 
$construct = " SELECT * FROM video WHERE $construct ";
$run = mysql_query( $construct );
$foundnum = mysql_num_rows($run);
if ($foundnum == 0) 
echo "Sorry, there are no matching result for <b> $search </b>. </br> </br> 
1. Try more general words. for example: If you want to search 'how to create a website' then use general keyword like 'create' 'website' </br>
2. Try different words with similar meaning </br> 
3. Please check your spelling"; 
else 
{$row = mysql_fetch_assoc($foundnum);
// echo "$foundnum results found !<p>"; 
while( $runrows = mysql_fetch_assoc( $run ) ) 
{
$title = $runrows ['title'];
$desc = $runrows ['description'];
$url = $runrows ['url'];
echo "<a href='$url'> <b> $title </b> </a> <br> $desc <br> <a href='$url'> $url </a> <p>"; 
}
}
} 
}
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP and Mysql search engine

Post by Christopher »

The PHP manual says, " mysql_query() returns a resource on success, or FALSE on error." I assume that your query is failing. Put error checking in this code to deal with query errors.

And please switch to mysqli or PDO. it is trivial to switch to mysqli. The mysql extension is not longer supported.
(#10850)
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: PHP and Mysql search engine

Post by pbs »

As you are using LIKE in foreach loop, means you will have multiple search text, so you need to use "OR" between multiple search options

try this

Code: Select all

$construct = "";
foreach($search_exploded as $search_each)
{
     $construct .= " keywords LIKE '%$search_each%' OR";
}
if ($construct != '')
     $construct = " AND (".trim($construct,"OR").")";
$construct = " SELECT * FROM video WHERE 1=1 $construct ";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run); 
Post Reply