Skip to content

Instantly share code, notes, and snippets.

@empirefx
Created August 8, 2019 23:33
Show Gist options
  • Save empirefx/f86bc123b1d3794adda22c9011f4e0c1 to your computer and use it in GitHub Desktop.
Save empirefx/f86bc123b1d3794adda22c9011f4e0c1 to your computer and use it in GitHub Desktop.
Mssql attempt conection
<?php
$host='tcp:172.18.0.3,1433';
$dbname='Master';
$sa='sa';
$pwd='yourStrong(!)Password';
try {
//The caveat is that disabling MARS means you can only have one active statement/result at a time. So depending on how your PHP code is structured you could encounter errors when executing a second statement or accessing a result.
$dbDB = new PDO("sqlsrv:Server=$host;Database=$database;MultipleActiveResultSets=false", $sa, $pwd);
//Fetching numeric data as strings may show a performance dip too. SQLSRV returns the data as its default PHP type by default, whereas PDO_SQLSRV returns the numeric data as strings. SQLSRV_ATTR_FETCHES_NUMERIC_TYPE flag can be used to fetch numeric data as its default PHP type when using PDO_SQLSRV.
$dbDB->setAttribute( PDO::ATTR_STRINGIFY_FETCHES,false );
$dbDB->setAttribute( PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE, true );
//If the code-page of the machine and user account under which the PHP process is running matches the server collation, SQLSRV_ENC_CHAR/PDO::SQLSRV_ENCODING_SYSTEM can be used to avoid conversions to UTF-8. These flags encode all character data into a one-byte-per-character 8-bit encoding that matches the local code-page.
$dbDB->setAttribute( PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_SYSTEM );
echo "<pre>";
foreach($dbDB->query('SELECT TOP 100 * FROM spt_monitor') as $fila) {
print_r($fila);
}
echo "</pre>";
$dbDB = null;
} catch (PDOException $e) {
echo "Could not connect to server";
echo "<pre>";
print_r($e);
echo "</pre>";
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment