Communication between PHP and SQL server using new WAMP approaches

What is the best way to create a connection between PHP and SQL Server, which are separate? (two servers: SQL server and PHP server b) notice that I am using wamp.

I read several articles as shown below , but I want to know if there is a new idea?

I am testing this code that works fine:

try{
  $user = 'user';
  $password = 'pass';
  $server="localhost";//or server IP
  $database="database";
  $conn = odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", $user, $password);
} catch (PDOException $e) {
  echo "Failed : " . $e->getMessage() . "\n";
  exit;
}
+1
source share
2 answers

I use the PDO_ODBC method:

1- Install ODBC on a server that has wamp and enable the PHP_PDO_ODBC extension on your disk

2- Use this code that supports UTF-8:

try{
  $hostname = "IP";
  $dbname = "database";
  $username = "username";
  $pw = "password";

  $pdo = new PDO ("odbc:Driver={SQL Server Native Client 10.0};Server=$hostname;Database=$dbname; Uid=$username;Pwd=$pw;");

} catch (PDOException $e) {
  echo "Failed : " . $e->getMessage() . "\n";
  exit;
}

$query = $pdo->prepare("select field_name from table");
$query->execute();

for($i=0; $row = $query->fetch(); $i++){
  echo iconv("CP1256","UTF-8",  $row['field_name'])."<br>";
}

3- replace these elements with yours:

IP-- --field_name-

4- "SQL SERVER" "SQL Server Native Client 10.0", "IP, " "IP", "ISO-8859-6" "CP1256".

+1

PHP: http://php.net/manual/en/pdo.construct.php

# 1 PDO

<?php
/* Connect to a SQL Server database using driver invocation */
$dsn = "sqlsrv:Server=12345abcde.database.windows.net;Database=testdb", "UserName@12345abcde", "Password";

try {
    $dbh = new PDO($dsn);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

HOST ip IP mysql.

+1

Source: https://habr.com/ru/post/1748274/


All Articles