Solved

Create HTML table with data from any query using sqlsrv

Posted on 2016-08-23
1
77 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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article discusses how to create an extensible mechanism for linked drop downs.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now