Display all fields & Data from MSSQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Display all fields & Data from MSSQL

Post 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
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

mssql_query

Post 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.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post 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 />";
}
//...
 
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Thanks OK

That worked!!!

Geoff
Post Reply