Solved

Create HTML table with data from any query using sqlsrv

Posted on 2016-08-23
1
100 Views
Last Modified: 2016-09-10
Hi

I am trying to create a piece of code to display the contents of a query in a table, using php, sqlserver and  sqlsrv_query.

I can build the body if the table, but the headers are not quite so easy. I don't believe that there is such a command as sqlsrv_fetch_field like the old mssql_fetch_field commend.


my code so far is...

//retrieve data from query...

$fields_num = mysql_num_fields($result);


echo "<table><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = sqlql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";

while($row = sqlsrv_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}



This produces the table body, but leaves the column names in the first row blank.

I'd appreciate any help!

thank you
0
Comment
Question by:rwlloyd71
1 Comment
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 41768374
mysql_num_fields  when using sqlsrv commands? should not work ,so use sqlsrv_num_fields instead
that should already be the main issue...

then, to get the name, you need to use this function:sqlsrv_field_metadata
http://php.net/manual/en/function.sqlsrv-field-metadata.php
code available on that page
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question