Solved

php + mssql: retreiving table returns duplicate colums.

Posted on 2011-03-08
2
556 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

743 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

10 Experts available now in Live!

Get 1:1 Help Now