homepage Welcome to WebmasterWorld Guest from 54.167.96.124
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Search Results ($num_rows) does not match $search results (PHP,MySQL)?
$num_rows shows more results than there are...
alcheme




msg:1265970
 10:57 pm on Jan 8, 2005 (gmt 0)

Hello, the $num_rows shows more results than there are...

For example, when I do a database search for a term I get 2 results but the $num_rows says there are 3 results.

I suspect it's because there are similar keywords within different search fields of the SQL statement. But I am stumped as to how I can change this. Any Suggestions?

-----> the code:

$result = mysql_query("SELECT wname, url, title, keywords, description, city, country, category FROM directory", $db);

$num_rows = mysql_num_rows($result);

if ($search) // perform search only if a string was entered.
{
mysql_connect() or die ("Problem connecting to Database");

$sql= mysql_query("SELECT wname, url, title, keywords, description, city, country, category FROM directory WHERE city LIKE '%$search%' wname LIKE '%$search%' title LIKE '%$search%' keywords LIKE '%$search%' description LIKE '%$search%' country LIKE '%$search%' category LIKE '%$search%' url LIKE '%$search%'", $db);

if ($sql)
{
echo "<b>Found <u>".$num_rows."</u> results to match search string: ".$search."</b><br><br>";

<----- End Code

I'll post the final solution when it works.

~Shane

 

coopster




msg:1265971
 11:43 pm on Jan 8, 2005 (gmt 0)

You are running two separate queries. First you have to decide from which result set do you want to get the number or rows returned. mysql_num_rows() [php.net] returns the number or rows for the result set you specify in the function.

alcheme




msg:1265972
 12:12 am on Jan 9, 2005 (gmt 0)

I tried this

$result = mysql_query("SELECT wname FROM directory", $db);

but it did not work... still shows 3 results when there are only 2.

alcheme




msg:1265973
 12:16 am on Jan 9, 2005 (gmt 0)

I just noticed it says I am getting 3 results even if there are no results or any number of results.

Hmmmm....

alcheme




msg:1265974
 12:22 am on Jan 9, 2005 (gmt 0)

Coopster... you totally make sense. Thank you! The $num_rows gets its results from $sql...

THE SOLUTION:

------Begin Code

if ($search) // perform search only if a string was entered.
{
mysql_connect() or die ("Problem connecting to Database");

$sql= mysql_query("SELECT wname, url, title, keywords, description, city, country, category FROM directory WHERE city LIKE '%$search%' wname LIKE '%$search%' title LIKE '%$search%' keywords LIKE '%$search%' description LIKE '%$search%' country LIKE '%$search%' category LIKE '%$search%' url LIKE '%$search%'", $db);

$num_rows = mysql_num_rows($sql);
if ($sql)
{
echo "<b>&nbsp;&nbsp;Found <u>".$num_rows."</u> results to match search string: ".$search."</b><br><br>";

------End Code

Take it easy!

~Shane

p.s. Now onto the next post... eek

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved