Php 函数中的空结果

Php 函数中的空结果,php,mysql,Php,Mysql,我正在建立一个分类网站,但每个类别都有自己的字段,当我生成字段时,会出现一个错误 NULL Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Applications/MAMP/htdocs/test.com/inc/db.inc.php on line 36 这是我使用的函数 function getCategoryFieldsById($cid) { $s

我正在建立一个分类网站,但每个类别都有自己的字段,当我生成字段时,会出现一个错误

NULL
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Applications/MAMP/htdocs/test.com/inc/db.inc.php on line 36
这是我使用的函数

function getCategoryFieldsById($cid)
{
    $sql = "select * from fields where categoryID = $cid order by fi_order";
    $result = $this->query($sql);
    $no     = $this->getNumRows($result);

    if($no)
    {   return $result; }
    else
    {
        $pid = $this->getParentId($cid);
        if($pid)
            $this->getCategoryFieldsById($pid); 
        else
            return 0;
    } 
}
问题是代表DB行返回NULL,我可以打印$no并看到有14行,但当进入if()时,它看不到数字

select * from fields where categoryID = 35 order by fi_order
0
select * from fields where categoryID = 34 order by fi_order
0
select * from fields where categoryID = 1 order by fi_order
14
NULL
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Applications/MAMP/htdocs/izomart.com/inc/db.inc.php on line 36
这是我的MySQL函数

function query($query) {
    $this->theQuery = $query;
    $result=mysql_query($query) or print(mysql_error());
    return $result;
}

function getNumRows($result){
    return mysql_num_rows($result);
}

当你递归调用一个最终应该返回一个值的函数时,当你进行递归调用时,你需要返回该值并传递该值

基本上,你所需要做的就是改变路线

$this->getCategoryFieldsById($pid); 


这将确保在找到值并返回到此调用时,此调用会将值返回到调用方法的原始位置。

您甚至没有向我们显示您在哪里使用
mysql\u
函数。您的
mysql\u fetch\u数组
语句在哪里?抱歉,我忘了添加它们,我现在添加了它们请检查我现在不需要获取行我只想返回行第一个值是35当没有行时获取父id并检查字段,,,大父id是1所以
$no=14
,,,,但是if()状态无法检查并返回$resultAs per answer edit,我认为这是因为要求category_id=null,这是无效语法,您需要要求when
category_id为null
Ahh,但没有注意到您正在递归调用函数。。我将编辑我的答案
return $this->getCategoryFieldsById($pid);