Juarez Posted July 31, 2013 Share Posted July 31, 2013 Hi. So I'm trying to echo out all the columns from the table. I'm connecting to SQL Server by the way. The number of columns in the table will vary so I want to loop and echo all the columns in the table without specifying. Its working but its echoing each column twice into the table. Any idea why this is? Thanks echo "<table border='1'> <tr> <th>start</th>,<th>end</th>" . join('</th><th>', $cols) . "</th> </tr>"; $tsql = "SELECT * FROM MYtable ORDER BY start"; /* Execute the query. */ $result = sqlsrv_query( $conn, $tsql); if ( $result ) { echo "Statement executed.<br>\n"; } else { echo "Error in statement execution.\n"; die( print_r( sqlsrv_errors(), true)); } while( $row = sqlsrv_fetch_array( $result)) { echo "<tr>"; foreach ($row as $field){ echo "<td>".stripslashes($field)."</td>"; } echo "</tr>"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/280675-echoing-each-column-from-table-twice-problem/ Share on other sites More sharing options...
Solution Muddy_Funster Posted July 31, 2013 Solution Share Posted July 31, 2013 sqlsrv_fetch_array returns two records per result line (as does mysql_fetch_array) - one with a numerical key and the other with an assoicative one, thus your wto reults being displayed. apply the fetch type operator to the function to fix this: while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC)) Quote Link to comment https://forums.phpfreaks.com/topic/280675-echoing-each-column-from-table-twice-problem/#findComment-1442811 Share on other sites More sharing options...
Juarez Posted July 31, 2013 Author Share Posted July 31, 2013 Great! thanks for the tip. Quote Link to comment https://forums.phpfreaks.com/topic/280675-echoing-each-column-from-table-twice-problem/#findComment-1442816 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.