在PHP 5.x中是否有任何简单的方法来回显RecordSet上的任何类型的数据?

So this is it.

I wrote a SQL statement dumb as duck, like:

SELECT * FROM table

Basically I want:

  1. Do a generic SELECT statement;
  2. Loop through the Resulset;
  3. Print the metadata "name" as Table Headers.
  4. Loop through Resultset again;
  5. Print each record as table row.
  6. Print each field as table column.
  7. 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.

You could specify the data type of the result value, and choose the string data type, as follows:

print_r (sqlsrv_get_field($stmt, $i, SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR)));

Alternatively, instead of calling sqlsrv_fetch and sqlsrv_get_field, use sqlsrv_fetch_array as that automatically converts all retrieved data to string.