Solved

php + mssql: retreiving table returns duplicate colums.

Posted on 2011-03-08
2
583 Views
Last Modified: 2013-12-13
Hello I am working with a apache, php, mssql server.

I've installed the microsoft  php_sqlsrv_53_ts_vc6.dll driver and the connection is working.

I am trying to make a page that displays the contents of a table or query.
For some reason, each column data is duplicated. But not the column header.

Any help would be appreciated!


//display the results 

    echo "<table border=1>"; 
	
/* Get and display field metadata. */
echo "<tr>"; 
foreach( sqlsrv_field_metadata( $result) as $meta)
{
		echo "<td>" . $meta["Name"] . "</td>";
}
echo "</tr>";
		
	// get column values 
    while($row = sqlsrv_fetch_array($result)) { 
        echo "<tr>"; 

		foreach ($row as $value)
		{
			if (is_object($value))
			{
			echo "<td>"."Object"."</td>";
			}
			else
			{
			//echo "<td>".gettype($value)."</td>";
			echo "<td>".$value."</td>";
			}
			
		}
			
		echo "</tr>"; 

    } 

    echo "</table>";

Open in new window

0
Comment
Question by:Darinwc
2 Comments
 
LVL 13

Accepted Solution

by:
dsmile earned 500 total points
ID: 35079534
Change line 14 to this

while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {

Since default fetch type of sqlsrv_fetch_array() is SQLSRV_FETCH_BOTH, you'll receive an array that have duplicated values for each row but different in keys.
Because you use foreach ($row as $value) to traverse the data, you'll received duplicated rows.
0
 
LVL 2

Author Closing Comment

by:Darinwc
ID: 35085193
Excellent!   I would have spent hours figuring that out!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

809 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