Skip to content

Instantly share code, notes, and snippets.

@openopen114
Last active June 16, 2016 08:37
Show Gist options
  • Select an option

  • Save openopen114/3d0db8f1c8415dd14ddbf40d0b00eec6 to your computer and use it in GitHub Desktop.

Select an option

Save openopen114/3d0db8f1c8415dd14ddbf40d0b00eec6 to your computer and use it in GitHub Desktop.
<?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