Skip to content

Instantly share code, notes, and snippets.

@makgithub
Created October 6, 2017 07:50
Show Gist options
  • Save makgithub/dde7e530294d108fc43438371eac79ee to your computer and use it in GitHub Desktop.
Save makgithub/dde7e530294d108fc43438371eac79ee to your computer and use it in GitHub Desktop.
PHP to Sql Server Db connection
<?php
$serverName = "*****.*****.****.**";//Host Name
$uid = "mtcdb";//User Name
$pwd = "******";//Password
$databaseName = "Mobile_Dev";//Database Name
$connectionInfo = array("UID" => $uid,
"PWD" => $pwd,
"Database" => $databaseName);
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn )
{
echo "Connected";
}
else
{
echo "<pre>";
die( print_r( sqlsrv_errors(), true));
}
//var_dump($conn);exit;
$tsql = "SELECT * FROM users";
/* Execute the query. */
$stmt = sqlsrv_query($conn, $tsql);
if ($stmt) {
echo "Statement executed.<br>\n";
} else {
echo "Error in statement execution.\n";
die(print_r(sqlsrv_errors(), true));
}
/* Iterate through the result set printing a row of data upon each iteration. */
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC)) {
// print_r($row);exit;
echo "Col1: " . $row[0] . "\n";
echo "Col2: " . $row[1] . "\n";
echo "Col3: " . $row[2] . "<br>\n";
echo "-----------------<br>\n";
}
/* Free statement and connection resources. */
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