doushouxie7064 2013-04-22 04:54
浏览 16
已采纳

空结果功能

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);
}
  • 写回答

1条回答 默认 最新

  • douchuxun4162 2013-04-22 04:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大