Skip to content

Instantly share code, notes, and snippets.

@Criipi
Last active July 16, 2016 19:01
Show Gist options
  • Save Criipi/8355f67698cf1bf1272f to your computer and use it in GitHub Desktop.
Save Criipi/8355f67698cf1bf1272f to your computer and use it in GitHub Desktop.
Shows how to connect to an Azure database using SQLSRV and PHP to get all of the data from a specified table and print each 'FIELDNAME' that we got from the db
<?php
if (!extension_loaded("sqlsrv"))
{
die("SQLSRV hasn't been loaded.");
}
// Connection details
$server = "tcp:<FULL SERVER ADDRESS>, <PORT>";
$user = "<DB USER>@<SERVER ADDRESS>";
$pwd = "<PASS>";
$db = "<DB NAME>";
$conn = sqlsrv_connect($server, array("UID"=>$user, "PWD"=>$pwd, "Database"=>$db));
// Ensure that the connection is successful.
if($conn == false){
die(print(sqlsrv_errors()));
}
// NOTE!: YOU NEED TO ADD THE SCHEMA NAME IN FRONT OF THE TABLE NAME!
$tsql = "SELECT * FROM SCHEMA.TABLENAME";
$stmt = sqlsrv_query( $conn, $tsql);
// If the statement fails, we'll kill the process.
if( $stmt === false)
{
echo "Error: \n";
die( print_r( sqlsrv_errors(), true));
}
// Print each 'FIELDNAME' value from the DB
while($row = sqlsrv_fetch_array($stmt))
{
echo ($row['FIELDNAME']);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment