Solved

Create HTML table with data from any query using sqlsrv

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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 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