如何检查php/sql数据库中while循环中选择的任何字段是否为0?检查是否有0
$res = mysql_query('SELECT id, name FROM table'); \\check here?
while($row = mysql_fetch_array)
{
\\or check here?
}
在此先感谢!
编辑: 我需要选择所有字段,然后检查是否它们中的任何一个是在查询0
如何检查php/sql数据库中while循环中选择的任何字段是否为0?检查是否有0
$res = mysql_query('SELECT id, name FROM table'); \\check here?
while($row = mysql_fetch_array)
{
\\or check here?
}
在此先感谢!
编辑: 我需要选择所有字段,然后检查是否它们中的任何一个是在查询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。否则,所有字段都是非零的。
是的!那是我正在寻找...至少有一个零!我的头如果只是如此F通过坐在电脑上好几个小时.. tahnks! – zorgyo 2010-11-02 13:07:27
添加where子句
SELECT id, name FROM table where my_field=0
,那么你并不需要检查每一个结果,你只会得到想要的结果行。
显然,检查while循环。对于查询可能会返回零行。
$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?
}
}
如果您确实必须返回所有行然后检查零,请在您的查询中添加一个order by子句以最小化您的检查。
SELECT id, name FROM table ORDER BY id DESC
然后
if ($id <= 0)
handleIt()
else
proceed()
请澄清情况请。发现的目的究竟是什么? – Gordon 2010-11-02 12:39:14
任何字段都是指id和name!为什么你想检查名称是零? – TechTravelThink 2010-11-02 12:40:15
你是否真的需要使用这些行,不管是否有零? – 2010-11-02 12:45:34