homepage Welcome to WebmasterWorld Guest from 54.167.179.48
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

    
No results found
wkpride

5+ Year Member



 
Msg#: 3870843 posted 5:46 pm on Mar 15, 2009 (gmt 0)

If I search for a match in the database, as in...

$result = mysql_query("SELECT * FROM table WHERE name LIKE '%$name%' and num LIKE '%num%'");
while($row = mysql_fetch_array($result))
$id = $row['sysid'];

My question is, what if no match is found? What will be the value of $id? null?

How do I test for no match?

Thanks! KP

 

wheelie34

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3870843 posted 6:14 pm on Mar 15, 2009 (gmt 0)

Hi

This should help

$amount = mysql_num_rows($result);
if the amount is greater than zero {
do something
}
else{
do something else
}

HTH

wkpride

5+ Year Member



 
Msg#: 3870843 posted 6:48 pm on Mar 15, 2009 (gmt 0)

Perfect.

Exactly what I needed. Most appreciated!

eeek

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3870843 posted 10:06 pm on Mar 16, 2009 (gmt 0)

LIKE '%$name%'

And of course you made sure to escape the $name string before you used it?

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3870843 posted 4:05 pm on Mar 17, 2009 (gmt 0)

If you don't want to add the extra query for num_rows or count(*), you can do

$results='';
$result = mysql_query("SELECT * FROM table WHERE name LIKE '%$name%' and num LIKE '%num%'");
while($row = mysql_fetch_array($result)) {
$id = $row['sysid'];
$results .= "<li> $id </li>\n";
}

if ($results != '') {
// output
}
else {
print "<p>No records found. <a href="add.php">Add one</a></p>\n";
}

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