Solved

Create HTML table with data from any query using sqlsrv

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses how to create an extensible mechanism for linked drop downs.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

746 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

13 Experts available now in Live!

Get 1:1 Help Now