Select one table with different number of table columns

I want to select a table from my SQL database. There may be many different columns in this table as I am compiling a dynamic query.

How to rewrite mine fetch_arrayin dynamic number of columns? Here is my current code:

$q = $db_object->query($query);
$returnstring = '';
while($r = $q->fetch_array()){
      $returnstring .= '<tr>';
      $returnstring .= '<td>'.$r.'</td>'; //Here can appear many different columns.
      $returnstring .= '</tr>'; 
}

In a query, for example, two, three, four or more columns of a table appear.

Is it possible to do foreachor something like this?

+4
source share
1 answer

This will cause the number to tdbe like the number of columns in your row.

$q = $db_object->query($query);
$returnstring = '';
while($r = $q->fetch_array()){
      $returnstring .= '<tr>';

      foreach($r as $key=>$value)
      {
        $returnstring .= '<td>'.$key.' : value='.$value.'</td>'; //Here can appear many different columns.
      }


      $returnstring .= '</tr>'; 
}
+4
source

All Articles