Hello, there is a small php script that I am converting from using mysql to mysqli, but I am having a bit of an issue with mysqli_fetch_fields (this is where I assume the problem lies). Basically it is a form that allows someone to enter free form queries in order to query the database. The results are printed in a nice data table. The results of the database table row return fine, but the column headings are not printing out. Instead the table headings are printing out like "Array Array", etc. Any help with this is greatly appreciated. The code is below.

if($type =="query") {

$query2 = str_replace("\\", " ", $query);

        if($result = DB::inst()->query("$query2"))
            _e( _t( "Successly Executed - " ) );
        else
            echo "<font color=red>Not able to execute the query<br>Either the 
                table doesnot exist or a wrong query.</font><br><br>";

        _e( _t( "Query is : " ) );
        echo("<font color=blue>".$query2."</font>\n");
        echo "<table class=\"dynamicTable tableTools table table-striped table-bordered table-condensed table-white\">
        <thead>
        <tr align=center>\n";

        $sds = $result->field_count;
        for($ss=0; $ss<$sds; $ss++)
        {
            $ee = $result->fetch_fields();
            echo "<td>$ee</td>";
        }
        echo "</tr>\n</thead>\n";

        $vv = true;
        while ($line = $result->fetch_assoc()) {
            if($vv === true){
            echo "<tr align=center>\n";
            $vv = false;
            }
            else{
            echo "<tr align=center>\n";
            $vv = true;
            }
            foreach ($line as $col_value) {
            echo "<td>".clean($col_value)."</td>\n";
            }
        echo "</tr>\n";
        }
        echo "</table>\n";
        /* Free resultset */
        $result->free_result();
    }
Member Avatar for diafol
$ee = $result->fetch_fields();

I believe that will return an array every time. You just need to run that statement once, get the array into the $ee variable and loop through it to create your table headers. From the manual (modded):

$ee = $result->fetch_fields();
foreach ($finfo as $val) {
    printf("<td>%s</td>\n", $val->name);
}

Thanks @diafol. But if I am reading your code correclty, $val->name may not be a column name for a particular database table. It is a free form (textarea) where the input can be different at anytime depending on the query that is being run.

Ah, nevermind, I missed the %s. Thanks for the solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.