Last active
June 16, 2016 08:37
-
-
Save openopen114/3d0db8f1c8415dd14ddbf40d0b00eec6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* Set Connection Credentials */ | |
| $serverName = "(local)"; //Local database | |
| $connectionInfo = array( "Database"=>"your_DatabaseName", "CharacterSet" =>"UTF-8"); // /* SET database name */ | |
| /* For Cloud Connection Credentials */ | |
| //$serverName = "192.168.1.4\SQLEXPRESS,1433"; // /* SET IP */ | |
| //$connectionInfo = array( "Database"=>"DB_name", "UID"=>"your_id", "PWD"=>"your_pwd" ,"CharacterSet" =>"UTF-8"); | |
| /* Connect using SQL Server Authentication. */ | |
| $conn = sqlsrv_connect( $serverName, $connectionInfo); | |
| if( $conn ) { | |
| echo "Connection established.<br />"; //connect OK | |
| }else{ | |
| echo "Connection could not be established.<br />"; //connect GG | |
| die( print_r( sqlsrv_errors(), true)); | |
| } | |
| /* SQL Query */ | |
| $tsql = "SELECT TOP 5 Name,Age FROM table001 "; | |
| $stmt = sqlsrv_query( $conn, $tsql); | |
| /* Process results JSON */ | |
| $json = array(); | |
| do { | |
| while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { | |
| $json[] = $row; | |
| } | |
| } while ( sqlsrv_next_result($stmt) ); | |
| echo json_encode($json,JSON_UNESCAPED_UNICODE); // not unicode | |
| /* write to json file */ | |
| file_put_contents('result_data.json',json_encode($json,JSON_UNESCAPED_UNICODE)) | |
| sqlsrv_free_stmt( $stmt); | |
| sqlsrv_close( $conn); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment