Skip to content

Instantly share code, notes, and snippets.

@anestan
Last active December 3, 2018 06:54
Show Gist options
  • Save anestan/3f8cfdbef085588f98c06ebd3b9150bc to your computer and use it in GitHub Desktop.
Save anestan/3f8cfdbef085588f98c06ebd3b9150bc to your computer and use it in GitHub Desktop.
connect php 7 to sql server 2014
# download ini di github
https://github.com/microsoft/msphpsql/releases
* sesuaikan dengan php versionnya, check di
http://localhost/dashboard/phpinfo.php
https://github.com/Microsoft/msphpsql/releases/download/v5.3.0/Windows-7.0.zip
* extract zip folder nya, lalu pilih folder sesuai tipe xampp nya. (tipe nya 32 bit di gw)
`php_pdo_sqlsrv_7_ts.dll` copy lalu taro di xampp/php/ext
* lihat detail di comment
@anestan
Copy link
Author

anestan commented Dec 3, 2018

  1. copy lalu taro di folder xampp/php/ext
    image

  2. lalu edit php.ini, tambahkan line sesuai nama extension yang sudah di download
    image

image

  1. restart apache, nanti liat di php info
    image

@anestan
Copy link
Author

anestan commented Dec 3, 2018

<?php

try {
    $dsn = "sqlsrv:Server=xxx;Database=xxx";
    $conn = new PDO($dsn, "sa", "xxx");
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch(Exception $e) {
    die( print_r( $e->getMessage() ) );
}
$tsql = "SELECT top 50 item, description FROM item_mst";
$getResults = $conn->prepare( $tsql );
$getResults->execute();
$results = $getResults->fetchAll(PDO::FETCH_BOTH);

foreach($results as $row) {
    echo $row['item'].' '.$row['description'];
    echo '<br>';
}

?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment