php连接sqlserver有三种方式
一:odbc连接,废话不多说直接上代码,封装了一个单例
<?php
/*** odbcServer.php* Author: Erekys*/namespace App\Model;
class odbcServer{public static $server;public static $username;public static $password;public static $database;// 静态私有变量,保存类的唯一实例 private static $instance = null; public function __construct () {$this->server = server;$this->username = username;$this->database = database;$this->password = password;}// 私有克隆方法,防止外部克隆 private function __clone() { } // 私有反序列化方法,防止外部反序列化 private function __wakeup() { } // 静态公有方法,用于获取类的唯一实例 public static function getInstance() { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } /*** db_con** 创建SqlServer连接*/public function db_con(){$server = $this->server;$username = $this->username; //数据库用户名$password = $this->password; //数据库密码$database = $this->database; //数据库@header("Content-Type:text/html;charset=GB18030"); try {// DSN 是 Data Source Name 的缩写,指定连接数据源的名称$dsn = "Driver={SQL Server};Server=192.168.0.106;Database=QHXJXdata;";// 使用 odbc_connect() 函数建立连接$conn = odbc_connect($dsn, 'sa', 'lhq119LHQ');return $conn;}catch (Exception $e) {echo "异常信息:", $e->getMessage();}}/*** db_query* 执行select语句,返回二维数组。*/public function db_query($sql, $fieldcount){$con = db_con();if (is_null($con))return null;$sql=iconv('UTF-8','GBK',$sql);$rs = odbc_exec($con, $sql);if( $rs === false) {//echo 'sql error : ' . $sql;//exit;}$table = [];if( $rs === false || odbc_num_rows($rs) == 0 ) {return $table;}while (odbc_fetch_row($rs)) {$row = [];$n = 0;while( $n < $fieldcount ) {$row[] = odbc_result($rs, ++$n);}$table[] = $row;}if( count($table) > 0 ) {odbc_free_result($rs);}odbc_close($con);return $table;}/*** odbc_exec* 执行insert,update或delete语句。* 如果执行不成功,调整一下数据库参数和odbc_connect参数。*/public function db_exec($sql){$con = db_con();if (is_null($con))return null;$dat = odbc_exec($con, $sql);odbc_close($con);return $dat;}
}
第二种:sqlserver,sqlserver需要php打开sqlserver扩展,windows下载扩展
sqlserver扩展地址 下载系统对应的64还是32位,还要看支持的php版本,下载下来的压缩包解压
然后把dll文件拷贝到php/ext下 php.ini加入extension=php_sqlsrv.dll.然后就可以用了\
$serverName = "your_server_address"; // 服务器地址
$connectionOptions = array("Database" => "your_database_name", // 数据库名"Uid" => "your_username", // 用户名"PWD" => "your_password" // 密码
);// 连接数据库
$conn = sqlsrv_connect($serverName, $connectionOptions);if ($conn === false) {die(print_r(sqlsrv_errors(), true));
}$sql = "SELECT * FROM your_table_name";
$stmt = sqlsrv_query($conn, $sql);if ($stmt === false) {die(print_r(sqlsrv_errors(), true));
}// 输出结果
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {print_r($row);
}// 释放资源和关闭连接
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
第三种:pdo_sqlsrv
需要下载php_pdo_sqlsrv扩展,php_pdo_sqlsrv下载地址
下载解压拷贝到php/ext下
try {// 连接字符串格式:"DRIVER={SQL Server Native Client 10.0};Server=你的服务器地址;Database=你的数据库名;Trusted_Connection=yes;"// 或者使用SQL Server 2012或更高版本的格式:"Server=你的服务器地址;Database=你的数据库名;Integrated Security=true;"$connectionString = "DRIVER={SQL Server};Server=your_server_address;Database=your_database_name;Trusted_Connection=yes;";$db = new PDO($connectionString);// 测试连接$stmt = $db->query("SELECT * FROM your_table_name");$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);print_r($rows); // 输出查询结果} catch (PDOException $e) {echo "数据库连接失败: " . $e->getMessage();
}