j son
j son

Reputation: 47

How to set min(value) variable as 0 when no value is returned by the sql query

$sql = "SELECT min(id) as sid FROM table1 where ....";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$sid = $row['sid'];

How can i set $sid as 0, when no value is returned by the sql query

(Here id in the table1 is int type)

Please help me....

Upvotes: 0

Views: 633

Answers (2)

dardar.moh
dardar.moh

Reputation: 6715

Try this:

$num_rows = mysql_num_rows($result);
if($num_rows == 0)
{
 $sid = 0;
}

mysql_num_rows return the number of rows for a query

Upvotes: 0

Marc B
Marc B

Reputation: 360602

If there are no matching rows, the min() function will return a NULL value, so use coalesce or ifnull() to turn it into a zero:

SELECT coalesce(min(id), 0) AS sid ...
SELECT ifnull(min(id), 0) AS sid ...

Upvotes: 2

Related Questions