Welcome to WebmasterWorld Guest from 54.146.55.156

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

No results found

     
5:46 pm on Mar 15, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 13, 2005
posts:63
votes: 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

6:14 pm on Mar 15, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:May 6, 2005
posts:863
votes: 0


Hi

This should help

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

HTH

6:48 pm on Mar 15, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 13, 2005
posts:63
votes: 0


Perfect.

Exactly what I needed. Most appreciated!

10:06 pm on Mar 16, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 20, 2004
posts:878
votes: 2


LIKE '%$name%'

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

4:05 pm on Mar 17, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 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";
}