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

Recommended Answers

All 2 Replies

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

you can also search help on mssql_field_name() in your case it could be sqlsrv_field_name()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.