So this is it.
I wrote a SQL statement dumb as duck, like:
SELECT * FROM table
Basically I want:
- Do a generic SELECT statement;
- Loop through the Resulset;
- Print the metadata "name" as Table Headers.
- Loop through Resultset again;
- Print each record as table row.
- Print each field as table column.
- How to test the data, in the field so I can properly echo this data as string? I'm not sure if I made myself clear.
I can't know what data will be returned form the statements, previously. This is motivated after trying print DateTime fields, and getting an error.
This is my PHP code:
$sql = "SELECT * FROM client";
$stmt = sqlsrv_query($conn, $sql);
if( $stmt === false)
{
die( print_r( sqlsrv_errors(), true));
}
$num_fields = sqlsrv_num_fields($stmt);
echo '<table class="table table-striped table-bordered">';
// Output Headers - In SQL Server
echo '<tr style="font-size:12px">';
foreach( sqlsrv_field_metadata( $stmt ) as $fieldMetadata )
{
foreach( $fieldMetadata as $name => $value)
{
if ($name == "Name"){
echo "<td><strong>" . "$value" . "</td></strong>";
}
}
}
echo "</tr>";
// Output fields data, after fetch new row.
while ( sqlsrv_fetch ( $stmt ) )
{
// \echo '<tr class="small" style="font-size:10px">';
echo '<tr class="small">';
for ($i = 0; $i < $num_fields; $i++)
{
echo '<td class="text-nowrap">';
print_r (sqlsrv_get_field($stmt, $i));
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
Please, correct me about any bad practice, or stupid coding... I'm a complete newbie here.