Solved

php + mssql: retreiving table returns duplicate colums.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

929 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

8 Experts available now in Live!

Get 1:1 Help Now