使用 WAMP 新方法连接 PHP 和 SQL 服务器 [英] Connection between PHP and SQL server using WAMP new approaches

查看:37
本文介绍了使用 WAMP 新方法连接 PHP 和 SQL 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在独立的 PHP 和 SQL 服务器之间创建连接的最佳方法是什么?(两台服务器:服务器 a SQL 和服务器 b PHP)请注意,我使用 wamp.

我阅读了一些类似下面的文章,但我想知道有什么新想法吗?

我测试了这段完美运行的代码:

尝试{$user = '用户';$password = 'pass';$server="localhost";//或服务器IP$database="数据库";$conn = odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", $user, $password);} catch (PDOException $e) {回声失败:".$e->getMessage() ."\n";出口;}

解决方案

我使用 PDO_ODBC 方法:

1- 在具有 wamp 的服务器上安装 ODBC 并在您的 wamp 上启用 PHP_PDO_ODBC 扩展

2- 使用支持 UTF-8 的代码:

尝试{$hostname = "IP";$dbname = "数据库";$username = "用户名";$pw = "密码";$pdo = new PDO("odbc:Driver={SQL Server Native Client 10.0};Server=$hostname;Database=$dbname;Uid=$username;Pwd=$pw;");} catch (PDOException $e) {回声失败:".$e->getMessage() ."\n";出口;}$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- 用您的替换这些项目:

IP-database-username-password-field_name-table

4- 有时您需要使用SQL SERVER"而不是SQL Server Native Client 10.0",有时您需要使用IP,port"而不是IP",有时您需要使用ISO-8859-6"代替CP1256".

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;
}

解决方案

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".

这篇关于使用 WAMP 新方法连接 PHP 和 SQL 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆