Jump to content

echoing each column from table twice problem


Juarez
Go to solution Solved by Muddy_Funster,

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

Link to comment
Share on other sites

  • Solution

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))
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.