php安装php_pdo_sqlsrv 和 php_sqlsrv 链接sqlserver数据库

景理
2023-12-01

windows系统

windows系统的拓展相对来说比较好安装,直接下载对应的dll文件,修改php.ini配置文件即可。

我本地环境是php5.6的,再网上找了好久才找到可以用的,分享链接给大家

链接: https://pan.baidu.com/s/1yNm4Cx8OVv44AwwzI4Ly-A

提取码: c5hk 

linux系统

环境安装参考:https://blog.csdn.net/BookNoteY/article/details/125503423

注意:一定要先安装

yum install msodbcsql mssql-tools unixODBC-devel
//如果是Ubuntu系统则只需 apt-get install msodbcsql mssql-tools unixODBC-devel

我用的wsl的Ubuntu系统,安装的时候报错

Unable to locate package msodbcsql

所以我参考 sql server - Unable to locate package msodbcsql - Stack Overflow 解决

执行了如下代码

sudo su 
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version


#Ubuntu 16.04
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list


exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install unixodbc-dev

php连接sqlserver代码

<?php
try{
//选择要连接的数据库
$dbName="sqlsrv:Server=120.192.***.**,1433;Database=WMS";//地址,端口号,数据库名称
//SqlServer数据库⽤户登录名称
$dbUser="sa";
//SqlServer数据库⽤户登录密码
$dbPassword="******";
//初始化⼀个PDO对象,判断是否连接成功
$db=new PDO($dbName,$dbUser,$dbPassword);
if($db)
{
echo"database connect succeed.<br />";
$stmt = $db->prepare("SELECT top 1 * FROM 表名");
$stmt->execute();
while ($row = $stmt->fetch()) {
var_dump($row);
}

}
}
catch(PDOException $e)
{
$content=iconv("UTF-8","gbk",$e->getMessage());
echo$content."<br />";
echo"database connect error";
}
?>

其他情况说明:

1.安装完毕链接数据库的时候报错

[Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_cl

解决参考:http://www.manongjc.com/detail/23-rfjltnulmryxfen.html

2. 安装过程报错

This extension requires the Microsoft ODBC Driver for SQL Server to communic

解决办法

curl https://packages.microsoft.com/config/rhel/6/prod.repo> /etc/yum.repos.d/mssql-release.repo
yum install -y msodbcsql mssql-tools unixODBC-devel

 类似资料: