Skip to content
PHP Logo

b. How can you format a table output using ADODB in PHP?

When retrieving data from a database using ADODB in PHP, you may want to format the output in a table for better readability. To do this, you can use a loop to iterate through the query results and output each row as a table row (<tr>), with each field as a table data cell (<td>). You can then echo out the table headers and rows to display the data in a tabular format.

php
// Assuming $rs is the ADODB recordset object containing the query results

echo "<table>";
echo "<tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr>";

while(!$rs->EOF) {
    echo "<tr>";
    echo "<td>".$rs->fields['column1']."</td>";
    echo "<td>".$rs->fields['column2']."</td>";
    echo "<td>".$rs->fields['column3']."</td>";
    echo "</tr>";
    $rs->MoveNext();
}

echo "</table>";