代码之家  ›  专栏  ›  技术社区  ›  SidC

mysql\u num\u fields():提供的参数不是有效的mysql结果资源

  •  0
  • SidC  · 技术社区  · 14 年前

    我正在开发一个定制的CMS,对DB模式和表示层进行了修改。使用下面的代码部分,我得到了一个与mysql\u num\u字段和mysql\u num\u行相关的错误。有人能告诉我为什么会出现这些错误吗?

    public function viewTableData($db_name,$table_name,$fld01,$where_clause,$order_by,$asc_desc){
    
               mysql_select_db($db_name);
    
            if($fld01!="")
            {
    
                $sql = "Select $fld01 from $table_name";
    
            }
            else
            {
    
                $sql = "Select * from $table_name";
    
            }
    
            if($where_clause!="")
            {
    
                $sql=$sql." ".$where_clause;
    
            }
    
            if(($order_by!="")&&($asc_desc!=""))
            {
    
                $sql=$sql." ".$order_by." ".$asc_desc;
    
            }
            else if(($order_by!="")&&($asc_desc==""))
            {
    
                $sql=$sql." ".$order_by;
    
            }
    
            //return "<br/>sql  :".$sql;
    
            $result = mysql_query($sql);
    
            $count_fields = mysql_num_fields($result); 
    
            $count_rows = mysql_num_rows($result);
    
            if($count_rows>0)
            {
            $index = 0;
    
                unset($this->data_array);
    
              while ($row = mysql_fetch_assoc($result)) {
    
                 $this->data_array[] = $row;
    
              } // while
    
        //Finally we release the database resource and return the multi-dimensional array containing all the data.
    
              mysql_free_result($result);
    
              return $this->data_array;
           }
    
        }
    
    2 回复  |  直到 13 年前
        1
  •  1
  •   Will A    14 年前

    打一巴掌 echo mysql_error(); mysql_query 行以查看来自服务器的错误。

        2
  •  0
  •   R. Hill    14 年前

    您不需要测试$result,从mysql\u query()返回时可能为FALSE

    另外,我会重写:

    if($order_by!="")
    {
    
        $sql.=" ".$order_by." ".$asc_desc;
    
    }
    

    MySQL不关心这里和那里的额外空间。