Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

php + mssql: retreiving table returns duplicate colums.

Posted on 2011-03-08
2
Medium Priority
?
634 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
[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
2 Comments
 
LVL 13

Accepted Solution

by:
dsmile earned 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

610 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