Welcome to WebmasterWorld Guest from 54.197.116.116

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

No results found

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

10+ Year Member



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)

WebmasterWorld Senior Member 10+ Year Member



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)

10+ Year Member



Perfect.

Exactly what I needed. Most appreciated!

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

WebmasterWorld Senior Member 10+ Year Member



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)

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



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";
}