search
top

MS SQL with PHP

Below is the simple PHP (>= 7.0.0) script to connect with the MS SQL database

<?php
try {
    $hostname = ""; //host name
    $port = 1433; //port number
    $dbname = ""; //database name
    $username = ""; //database username
    $pw = ""; //database password
    $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }

$sql = ""; //query 
$stmt = $dbh->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch()) {
print_r($row);
}
?>

Hope this is helpful.

Leave a Reply

top