Page 1 of 1

Display all fields & Data from MSSQL

Posted: Mon Dec 04, 2006 9:48 am
by JimiH
Hi

I have this code which displays a particular field ("field1") from a MSSQL database

Code: Select all

<?php 
error_reporting(E_ALL);

$server="server";
$username="administrator";
$password="***";
$db = "SQL";
$table = "table";

$sqlconnect=mssql_connect($server, $username, $password);
$sqldb=mssql_select_db($db,$sqlconnect);
$sqlquery="SELECT * FROM $table;";
$results= mssql_query($sqlquery);
while ($row=mssql_fetch_array($results)){
echo $row['field1']."<br>\n";}
mssql_close($sqlconnect);

?>
How do I display all the fields in the DB along with their corresponding data.

Thanks

Geoff

mssql_query

Posted: Mon Dec 04, 2006 11:28 am
by timclaason
I don't think there's a built in variable to output all variables. I think you'll just need to do $row['field1'], $row['field2'], etc.

Unless anyone else knows of any better way to do it.

Posted: Mon Dec 04, 2006 12:38 pm
by ok
Try:

Code: Select all

//...
$results = mssql_query($sqlquery);
$number_of_fields = mssql_num_fields($results);
while($row = mssql_fetch_row($results))
{
 for($i = 0; $i < $number_of_fields; $i++)
 {
  echo mssql_field_name($results, $i);
 }
 echo "<br />";
 for($i = 0; $i < count($row); $i++)
 {
  echo $row[$i ]."  ";
 }
 echo "<br />";
}
//...
 

Posted: Tue Dec 05, 2006 5:22 am
by JimiH
Thanks OK

That worked!!!

Geoff