Q:

Null Result in function

I'm building a classified website but each category has its own fields, In place an add page when I'm generating the fields an error appeared
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

this is the function i used
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;
    } 
}

the problem is return NULL on behalf of the DB rows, I can print the $no and see there are 14 rows but when go inside if() it can't see the number
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

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

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

A:

When you call a function recursively that is supposed to in the end return a value, when you make the recursive call you need to return that value and pass is up the chain.
Basically, all you need to do it change the line
$this->getCategoryFieldsById($pid); 

to
return $this->getCategoryFieldsById($pid); 

This will make sure that when the value is found, and returned to THIS call, THIS call returns the value to the original place the method was called from.

