PHP Code Snippet for Troubleshooting PHP/SQL Server Issues
This code snippet will help you troubleshoot connection issues between PHP and SQL Server. To use it, replace the bracketed information with the information from your environment. Then save it to your document root, and open it with a browser.
<?php // edit the lines between here and the next comment using values from your installation $DBName = "[YOUR_DATABASE_NAME]"; $user = "[YOUR_USER_NAME]"; $pass = "[YOUR_PASSWORD]"; $host = "[YOUR_HOSTNAME]"; $stmt = "[YOUR_SELECT_STATEMENT]"; // edit the lines between here and the previous comment $conn = DBConnect($DBName, $user, $pass,$host); echo "$conn <br>"; DBError(); $userData = sqlsrv_query($conn,$stmt, array(), array("Scrollable"=>"buffered")); DBError(); $gotrows = sqlsrv_has_rows($userData); if (!$gotrows) { $e = "WARNING No Rows found for SQL statement $stmt"; echo "$e <br>"; } While( $row = sqlsrv_fetch_array( $userData, SQLSRV_FETCH_ASSOC) ) { $t = $row['returnoptions']; echo "Selected Value => $t<br>"; } $d = DBDisconnect($conn); DBError(); ////////////////////////////////////////PHP Functions Below This Line function DBDisconnect($conn) { $rtn = sqlsrv_close( $conn ); if ($rtn) { $result = "SUCCESS $conn"; } else { $result = "ERROR $conn"; } echo $result; return $result; } function DBError() { //echo "entering dberror"."<br />"; $errmessage = ""; $rtn = false; // returns true if no errors are found, else false if( ($errors = sqlsrv_errors() ) !== null) { $rtn = true; //there was an error foreach( $errors as $error ) { $errmessage = "SQLSTATE ERROR: ".$error[ 'SQLSTATE'];echo $errmessage; $errmessage = "code: ".$error[ 'code']; echo $errmessage; $errmessage = "message: ".$error[ 'message']; echo $errmessage; } } return $rtn; } function DBConnect($DBName, $user, $pass, $host){ //include_once 'settings/settings.php'; //$DBName = 'ReliableDB_70'; //$user = 'ReliabilityUser'; //$pass = 'Goodman2!'; //$host = "localhost"; $connectinfo = array( "Database"=>$DBName, "UID"=>$user, "PWD"=>$pass); //print_r ($connectinfo); $r = sqlsrv_connect( $host, $connectinfo); if( ($errors = sqlsrv_errors() ) !== null) { $rtn = true; //there was an error foreach( $errors as $error ) { $errmessage = "SQLSTATE ERROR: ".$error[ 'SQLSTATE']; echo $errmessage."<br>"; $errmessage = "code: ".$error[ 'code']; echo $errmessage."<br>"; $errmessage = "message: ".$error[ 'message']; echo $errmessage."<br>"; } } return $r; } ?>