Solved

php + mssql: retreiving table returns duplicate colums.

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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article discusses how to implement server side field validation and display customized error messages to the client.
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 shrink a transaction log file down to a reasonable size.

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