[英]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 当sql查询没有返回任何值时,如何将$ sid设置为0
(Here id in the table1 is int type) (这里table1中的id是int类型)
Please help me.... 请帮我....
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: 如果没有匹配的行,则min()函数将返回NULL值,因此使用coalesce
或ifnull()
将其转换为零:
SELECT coalesce(min(id), 0) AS sid ...
SELECT ifnull(min(id), 0) AS sid ...
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 mysql_num_rows返回查询的行数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.