Link to home
Start Free TrialLog in
Avatar of CCC-Ravens
CCC-Ravens

asked on

Retrieving values from MSSQL and display them in PHP

I am trying to return data from a MSSql server and It seems to be connecting to the database ok but I am running into issues getting and displaying information here is the code I am trying to use keep in mind I have never used MSSQL I have always used MYSQL and so I may be way off base here.

<?php
$serverName = "myserver\myinstance"; //serverName\instanceName
$connectionInfo = array( "Database"=>"play", "UID"=>"user", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
	 $strsql = "SELECT MAX(ID_NUM)FROM NAME_MASTER WHERE (ID_NUM < 999954)";
	 $sqlquery = sqlsrv_query( $conn, $strsql);
}
 if($sqlquery)
{
      echo "Statement executed.\n";
	 while($row = sqlsrv_fetch_array($sqlquery)) {
      echo $row['Id_Num'];
}
	  
} 
else
{
      echo "Error in statement execution.\n";
      die( print_r( sqlsrv_errors(), true));
}

/* Free connection resources. */
sqlsrv_close( $conn);
?>

Open in new window

Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

What error do you see ?
Avatar of CCC-Ravens
CCC-Ravens

ASKER

It says the connection is sucsessful and that the statement ran, but I am not sure it did and it doesn't return the value that should be returned fron the sql statement. But no accual error is being displayed.
I have changed my code for troubleshooting, I am tring to display more information to see what is working and what is not in hopes I can see where the code is failing. however I have reached a bit of an impass as I cannot figure out quite what the code is doing.
<?php
// get SSN from form



// Check SSN against MsSql Server to see if it already exists

$serverName = "Myserver\Myinstance"; //serverName\instanceName
$connectionInfo = array( "Database"=>"TmsEPly", "UID"=>"un", "PWD"=>"pw");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
	 //$strsql = "SELECT * FROM BIOGRAPH_MASTER WHERE (SSN = ' " . $_POST[SSN] . " ')";
	$strsql = "SELECT SSN FROM BIOGRAPH_MASTER WHERE (SSN = ' " . $_POST[SSN] . " ')";
	 $sqlquery = sqlsrv_query( $conn, $strsql);
	 $rowCount = sqlsrv_num_rows( $strsql );
	 echo $rowCount;

}
 if( $sqlquery != null )
{
      echo $_POST[SSN];
	  echo $sqlquery;
	 while($row = sqlsrv_fetch_array($sqlquery)) {
      echo $row['SSN']."\n";
	  echo "Statement executed.\n";
	}  
} else {
      echo "Error in statement execution.\n";
      die( print_r( sqlsrv_errors(), true));
}

/* Free connection resources. */
sqlsrv_close( $conn);

?>

Open in new window


it is echoing "Connection established", it is echoing the $_POST[SSN] "123456789" but on the line  echo $sqlquery I get Resource id #2 instead of the expected result ans I cannot get a row count at all.

I am at a loss as to why this is not working, can anyone help me on this issue?
I turned display errors on in the php.ini and now I see

Notice: Use of undefined constant SSN - assumed 'SSN' in E:\jics\CCC\includes\check_SSN.php on line 15 Warning: sqlsrv_num_rows() expects parameter 1 to be resource, string given in E:\jics\CCC\includes\check_SSN.php on line 17 Notice: Use of undefined constant SSN - assumed 'SSN' in E:\jics\CCC\includes\check_SSN.php on line 23 1234567789Resource id #2

Does this shed some light on the problem?
ASKER CERTIFIED SOLUTION
Avatar of CCC-Ravens
CCC-Ravens

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
cause I figured it out and no one else did