Hi guys

Ive created a piece of software to extract data from 2 fields in an mssql table, and using php translate them into a graph. Im now trying to extract the field names as titles for my axes. I'm using sqlsrv_query s and have retreived data from the table no problem, however I am finding it difficult to get field names.

I have tried the code:

$result = sqlsrv_query($conn, "SELECT Column_Name FROM information_schema.columns where table_name = 'timeSightings'");
$row = sqlsrv_fetch_array($result);

while($row = sqlsrv_fetch_array( $result )) 
 Print "<b><br>name of column one:</b> ".$row[0] . " "; 
 Print "<b><br>name of column two:</b> ".$row[1] . " <br>"; 

name of column one: sightings
name of column two:

There are 2 errors in the output
1: column one name should be 'time' and column 2 should be 'sightings'
2: only 1 column name is displayed

Any ideas how to do this?

Thanks, David

Edited by mdpepper: n/a

6 Years
Discussion Span
Last Post by urtrivedi

Fixed. The problem lies in the while statement. I altered the while to a do ... while If anyone has a similar issue this is my solution:

$stmt = sqlsrv_query( $conn, "SELECT Column_Name FROM information_schema.columns where table_name = 'timeSightings' " , array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
	$row = sqlsrv_fetch_array($stmt);
    $row_count = sqlsrv_num_rows( $stmt );
	if ($row_count === false)
		echo "\nerror\n";
	else if ($row_count >=0)
		echo "\nRow count: $row_count\n";

	$i = 0;
			$fieldNameInfo[$i] = $row[0];
			//echo "info".$row[0];
		}while($row = sqlsrv_fetch_array($stmt)) ;

	echo "<br>fieldNameInfo[0] = ".$fieldNameInfo[0];
	echo "<br>fieldNameInfo[1] = ".$fieldNameInfo[1];

Row count: 2
fieldNameInfo[0] = time
fieldNameInfo[1] = sightings

Edited by mdpepper: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.