Php connection to SQL Server

0

How can I connect to php with sqlserver (pdo)? It sounds simple, but I can not ... Already enabled the driver everything ok Attached is the name of the server, I'll run it on the site ...

    
asked by anonymous 10.02.2017 / 00:08

2 answers

4

You have two options to try:

$pdo = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");

or

$pdo = new PDO( "sqlsrv:server=$serverName ; Database=$dbname", "$username", "$pw");  

It depends on the driver you installed in PHP.

[Edited] An example of a select with PDO

//retornar o resultset
$sql = 'Select * from products';
$result = $pdo->query($sql);

// imprimir resultados um a um 
while ($campos = $result->fetch()){
    echo $campos['ProductID']. ' - '.$campos['ProductName'].'<br />';
}
    
10.02.2017 / 00:31
1

Good evening. If you do not have the extensions sqlsrv and pdo_sqlsrv, it is best to try with odbc. Here's how:

$pdo = new \PDO ("odbc:Driver={SQL Server Native Client 11.0};Server=localhost;Database=catalogName;app=appName;WISD={$_SERVER['REMOTE_HOST']}",$sqlUser,$sqlPass);

What version of PHP are you using? Can you send a print of phpinfo in the PDO area similar to the image below? See if in PHP configuration is enabled pdo_sqlsrv, because it seems to me that this driver you already have.

    
10.02.2017 / 01:06