tags:

views:

30

answers:

2

Happy Friday All,

I'm working on a custom CMS, made changes to the DB schema and presentation layer. I'm getting an error relative to mysql_num_fields and mysql_num_rows using the following section of code. Can someone give me an idea of why these errors are being raised?

Thanks, Sid

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;
       }

    }
+1  A: 

Slap an echo mysql_error(); line after the mysql_query line to see the error from the server.

Will A
Here's the response I received:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 6' at line 1However, on Line 1 of this php file, there is nothing written relative to Limit 0.6. Ideas?
SidC
Slap an echo $sql just before the query to make sure the query is what you think it is.
Brian Hooper
The plot thickens gang:After echoing $sql, I now have an error stating select * from invoice where id = Limit 0,6.Ideas on where the Limit 0,6 is coming from given above snippet?Thanks!
SidC
`echo $where_clause;` and `echo $order_by;` somewhere in the function - one or other of these (probably the former) sounds like the culprit.
Will A
A: 

You don't test $result, which can be FALSE upon return from mysql_query()

Also, I would rewrite:

if($order_by!="")
{

    $sql.=" ".$order_by." ".$asc_desc;

}

MySQL doesn't care about extra spaces here and there.

R. Hill