1

what is the best way for create a connection between PHP and SQL server that are seperate?(two server: server a SQL and server b PHP) notice that I use wamp.

I read some articles like below but I want to know is there any new idea?

I test this code that works perfectly:

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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
rezaSefiddashti
  • 134
  • 1
  • 9
  • 1
    Use PDO with a conenction string that is configured to connect to ANY sql server... – Cagy79 Dec 14 '16 at 15:21
  • thanks @Cagy79 . please give me a link for more help. – rezaSefiddashti Dec 14 '16 at 16:14
  • Possible duplicate of [Connect to SQL Server through PDO using SQL Server Driver](http://stackoverflow.com/questions/12747554/connect-to-sql-server-through-pdo-using-sql-server-driver) – ICE Dec 20 '16 at 19:51

2 Answers2

1

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

Example #1 Create a PDO instance via driver invocation

<?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();
}

?>

Just change the HOST ip to the IP and port of your mysql server.

Cagy79
  • 1,610
  • 1
  • 19
  • 25
  • thanks.I have a question: do you connect to mysql? I want to connect to sql server. – rezaSefiddashti Dec 14 '16 at 16:12
  • The procedure for a Microsoft SQL Server is similar with PDO, however the DSN is different. Take a look at the documentation for PDO connections with MSSQL: http://php.net/manual/en/ref.pdo-sqlsrv.connection.php – Striezel Dec 14 '16 at 22:50
  • Edited the answer to reflect sqlserver instead of mysql. Please accept the answer if it suited your needs. – Cagy79 Dec 15 '16 at 12:41
  • I use a pdo ODBC method for connect to sql server. and I think you should install a software like odbc for sql server and then connect to it. @Cagy79 thanks – rezaSefiddashti Dec 17 '16 at 08:01
1

I use PDO_ODBC Method:

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

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 Items with yours:

IP-database-username-password-field_name-table

4- sometimes you need use "SQL SERVER" instead of "SQL Server Native Client 10.0" and sometimes you need use "IP,port" instead of "IP" and sometimes you need use "ISO-8859-6" instead of "CP1256".

sdi
  • 26
  • 2