Reputation: 59575
I need a way to do this, If a mysql query dosn't retrieve any data, something happens. Here's an example.
$color="red";
$query = mysql_query("SELECT * FROM people WHERE shirt='$color'");
if($query = null){
echo 'No people wearing '.$color' shirts available.';
}
Upvotes: 1
Views: 2993
Reputation: 300
You could use empty
either:
$num_rows = mysql_num_rows($result);
if(empty($num_rows)){
echo 'No results';
}
Upvotes: 0
Reputation: 188194
Use mysql_num_rows
to check, how many rows have been returned by your query.
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
if( $num_rows == 0 ) {
// No results returned
echo "No results!";
} else {
echo "$num_rows Rows\n";
}
?>
Upvotes: 5
Reputation: 9661
Besides the mysql_num_rows()
there is also COUNT()
which could be used like this:
"SELECT COUNT(*) FROM people WHERE shirt='$color'"
You might actually need more data from the database but if you only need a number then this would remove the need for an if
.
Upvotes: 0
Reputation: 11628
Use mysql_num_rows()
for this.
if( mysql_num_rows($query) == 0 ) {
// No results returned
Upvotes: 9