【发布时间】:2011-04-16 04:01:46
【问题描述】:
我有一个函数,它只返回一行作为数组。
我给出一个查询作为参数,它只会给出一行。
function getFields($query)
{
$t =& get_instance();
$ret = $t->db->query($query);
if(!$ret)return false;
else if(!$ret->num_rows()) return array();
else return $ret->row_array();
}
$ret = getFields('query string');
我以为是……
- 如果有错误,我可以像 if(!$res)//echo 错误一样检查它
- 如果它是空的,我可以像 if(!count($res))// no rows 一样检查它
- 否则我假设有一行并继续该过程...
但是
- 出错时返回 false。在 if(count($ret)) 中给出 1。
- 如果我作为空数组返回,如果 ($ret) 条件失败(返回 false)。
//
$ret = getFields('query string');
if(!$fes)jerror('status,0,msg,dberror');
if(!count($fes))jerror('status,1,msg,no rows');
// continue execution when there atleast one row.
此代码是使用 ajax 调用的。所以我返回一个 json 响应。
为什么 count 给出 1,如果空数组给出 false。
我只是想用逻辑条件编码而不是给出更多的关系条件。只是为了减少代码。
我在哪里可以得到所有这些 php 的 BUGGING 东西,这样我就可以确保我最终不会犯像上面这样的逻辑错误。
BUGGING - 在上面的句子中 我所说的窃听不是错误,而是 事情困扰着我们。使我们 逻辑错误。
我编辑了以下代码以包含以下内容,同时我得到了https://stackoverflow.com/users/451672/andrew-dunn的回复@
我可以这样做,但我仍然想知道上述解释的原因
if($fes===false)jerror();
if(!$fes)jsuccess('status,4');
【问题讨论】:
标签: php