检查是否为0

时间:2010-11-02 12:29:40

标签: php sql

如果在while循环中选择的数据库中的任何字段为0,如何检查php / sql?

$res = mysql_query('SELECT id, name FROM table'); \\check here?
while($row = mysql_fetch_array)
{
 \\or check here?
}

提前致谢!

编辑: 我需要选择所有字段,然后检查它们中的任何一个是否为0.

5 个答案:

答案 0 :(得分:0)

在查询中添加一个where子句

SELECT id, name FROM table where my_field=0

然后你不需要检查每个结果,只得到想要的结果行。

答案 1 :(得分:0)

显然,检查while循环内部。对于查询可能会返回ZERO行。

答案 2 :(得分:0)

$foundZero = false;
$res = mysql_query('SELECT id, name FROM table');
while ($row = mysql_fetch_array($res))
{
    if (in_array(0, $row))
    {
        // This row has a zero
        $foundZero = true;
    }
}

if ($foundZero)
{
    // at least one zero in one row has been found
}
else
{
    // No zeros have been found
}

如果$foundZero为真,则一行中至少有一个字段等于0.否则,所有字段都不为零。

答案 3 :(得分:0)

$res = mysql_query('SELECT count(*) FROM table WHERE my_field=0'); \\check here?
if(mysql_num_rows($res) > 0)
{
    while($row = mysql_fetch_array)
    {
         \\or check here?
    }
}

答案 4 :(得分:0)

如果您确实必须返回所有行,然后检查零,请在查询中添加order by子句,以尽量减少检查。

SELECT id, name FROM table ORDER BY id DESC

然后

if ($id <= 0)
   handleIt()
else
   proceed()
相关问题