4

I want to select a table from my SQL database. This table can have many different columns, as I'm putting together a dynamic query.

How can I rewrite my fetch_array to a 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 the query could e.g. two, three, four or more columns of the table show up.

Can I do a foreach or something like this?

4

1 回答 1

4

这将使tds 与行中的列数一样多。

$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>'; 
}
于 2013-10-14T08:03:24.243 回答