Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

First PHP Sql Query - Not Working - No Results or Errors

Status
Not open for further replies.

misscrf

Technical User
Jun 7, 2004
1,344
US
I'm trying to just make a simple connection, so I can learn how to query info from sql server on a php page. So far, none of my attempts are working at all. I get no errors, no results, nada.
Below is my code, if anyone can help me figure this out. Thanks!

Code:
	<?php
		echo "<h2>Project Group Key:</h2>";
		echo $project;
		echo "<br>";
		$connectionInfo = array("Database"=>"123456ProjectDB","ReturnDatesAsStrings" => true);
		$db = sqlsrv_connect("lit-sql",$connectionInfo) or die("Unable to connect to server");
		$result = sqlsrv_query($db,"SELECT [projectkey] FROM [123456ProjectDB].[dbo].[vw_projectgroups]");
		
		function get_error() {
			$errors = sqlsrv_errors();
			$errtxt = "Error occured: ";
			foreach( $errors as $error) {
				$errtxt .= $error["SQLSTATE"] . ", ";
				$errtxt .= $error["code"] . ", ";
				$errtxt .= $error["message"] . "<br>";
			}
			return $errtxt;
		}
		while ($row = sqlsrv_fetch_array($result)) {
			if($row===false){ die(get_error()); }
				else ( print_r($row); }
}
		$row_count = sqlsrv_num_rows( $result );
		if ($result !== NULL) 
			{$rows = sqlsrv_has_rows( $result );
			if ($rows === true)
				echo "\nthere are \n$row_count\n rows\n";
		else
			echo "\nno rows\n";}
		$j=1;
		while ($row = sqlsrv_fetch($result)) {$ColName = sqlsrv_get_field( $result, 0);
		print "<BR>";
		echo "Row: ". $j . " " . $ColName;
		$j=$j+1;}
		sqlsrv_free_stmt( $result);
		sqlsrv_close( $db);
	?>

misscrf

It is never too late to become what you could have been ~ George Eliot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top