Jump to content

echoing each column from table twice problem


Juarez

Recommended Posts

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>";

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))

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.