Saturday, 4 March 2017

MSSQL server connect with PHP

<?php
//donwload the drivers for the server
// https://github.com/Microsoft/msphpsql/releases
//https://www.youtube.com/watch?v=hxfdbnpOqSI
/**
STEP 1: x86
STEP 2: path/php/ext/<extract "extension=php_pdo_sqlsev_7_ts.dll"> file  in php.ini

STEP 3: Restart server

STEP 4: CODE
*/
try{
 $conn = new PDO("sqlsrv:Server=127.0.0.1,1443;Database=test","test","test");
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e){
 die(print_r($e->getMessage()));
}

$tsql = "select * from table";
$getResults = $conn->preoare($tsql);
$getResults->execute();
$results = $getResults->fetchAll(PDO::FETCH_BOTH);

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

No comments:

Post a Comment