?
Solved

Create HTML table with data from any query using sqlsrv

Posted on 2016-08-23
1
Medium Priority
?
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article discusses how to implement server side field validation and display customized error messages to the client.
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
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

765 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