centos mssql pdo驱动及配置
首先了解一下PDO的一些基本知识:
PDO是PHPDateObject的简称,它是PHP 5.1版本一起发行的,目前支持的数据库包括Firebird,FressTDS,MySQL,Ms SQL Server,ODBC,Oracle等。
有了PDO,您不必再使用mysql_*函数,oci_*函数或者mssql_*函数,也不必再为它们封闭数据库操作类,只需要使用PDO接口中的方法就可以对数据库进行操作,在选择不同的数据库时,只修改PDO的DSN即可。
PDO是一个数据库访问抽象层,作用是统一各种数据库的访问接口,与MYSQL和MSSQL函数库相比,PDO让跨数据库的使用更具亲和力,与ADODB和MDB2相比,PDO更高效。
PDO将通过一种轻型,清晰,方便的函数,统一各种不同的RDBMS库的共有我,实现PHP脚本在最大程序上的抽象性和兼容性。
PDO吸取了现有数据库扩展成功和失败的经验教训,利用PHP5的最新特性,可以轻松地与各种数据库进行交互。
PDO扩展是模块化,能够在运行时为用户数据库后端加载驱动快,而不必重新编译或重新安装整个PHP程序。
安装必要的扩展unixODBC 和 freeTDS
1、cd /root 【进入自己的目录,我的是root】
2、yum -y install unixODBC unixODBC-devel 【说明:安装ODBC驱动,然并卵,mssql的驱动并不在这里,默认包含了mysql的odbc驱动,安装mssql的liunx驱动则需要借助第三方扩展包freeTDS】
3、wget -c ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz 【下载freeTDS】
4、tar -zxf freetds-patched.tar.gz 【解压下载的文件】
5、cd freetds-1.00.21 【进入freetds目录,我下载的时候,该扩展是1.00.21版】
6、./configure –prefix=/usr/local/freetds –with-tdsver=7.3 –with-unixodbc=/usr –enable-msdblib 【/usr/local/freetds:我计划要安装的位置,7.3是支持的sqlserver版本为sql server 2008 ,点击这里查看TDS对应的版本。 –enable-msdblib是是否允许Microsoft数据库函数库,这里为允许】
7、make && make install 【编译安装freeTDS】
8、cd /usr/local/freetds/bin 【进入freetds目录,测试一下是否安装好了】
9、./tsql -C 【查看freetds是否安装好了,这个命令显示了你的初始freetds配置】
常见错误:configure: error: Cannot find FreeTDS in known installation directories
解决方法:新建两个空文件即可
$ touch /usr/local/freetds/include/tds.h
$ touch /usr/local/freetds/lib/libtds.a
odbc中freeTDS的配置
1、cd /usr/local/freetds/etc
2、vi freetds.conf
3、添加一下mssql的配置
# A typical Microsoft server
[Server2008] //自己起的名字,我这里起名Server2008,在odbc.ini中需要用到。
host = 127.0.0.1 # 要连接的sql server服务器IP
port = 1433 # 要连接的sql server服务器端口
tds version = 7.3 # 要连接的sql server 版本
client charset = utf8 # 如果乱码可以加上这一句
4、然后配置一下odbc.ini 和 odbcinst.ini ,这两个文件在/etc 目录下。
5、先查找find / -name libtdsodbc.so 和 find / -name libtdsS.so,查找出路径备用。
odbcinst.ini配置如下【在文件后面添加,前面默认是mysql的odbc连接】:
# Driver from the mssql-connector-odbc package
# Setup from the freeDTS package
[MSSQL]自己起名,odbc.ini中要用
Description = v7.3 for 2008server
Driver = /usr/local/freetds/lib/libtdsodbc.so
Driver64 = /usr/local/freetds/lib/libtdsodbc.so
Setup = /usr/lib64/libtdsS.so
Setup64 = /usr/lib64/libtdsS.so
UsageCount = 1
CPTimeout =
CPReuse =
红色部分是你自己的so模块文件的真实路径。
odbc.ini配置如下:
[my_mssql]
Description = My SQL_server link
Driver = MSSQL #和odbcinst.ini中对应
Servername = Server2008 #和freeDTS中的配置相对应
Database = 默认的数据库名
UID = 用户名
PWD = 密码
Port = 端口
6、这时,你可以测试一下是否可以通过odbc的dns方式连接数据库了。
测试freedts:/usr/local/freetds/bin/tsql -S Server2008 -U root 【你自己的配置内容和安装路径】
或者用odbc的isql方式连接测试: isql -v my_ssql 用户名 密码 参数
[my_mssql是odbc.ini中配置的名字,参数可选]
1 2 3 4 5 6 7 8 9 10 |
isql DSN [UID [PWD]] [options] DSN 数据源名称 UID 用户ID PWD 用户密码 Options: -b 批处理,没有提示符的模式 -dx 设置列之间的分隔符为x -w 将查询结果输出为HTML格式 -c 第一行输出列名 --version 输出isql的版本号 |
让php可以使用odbc连接数据库
1、需要php源码,我的php是5.6版本的,用的是军哥的lnmp套件,可以直接 cd lnmp1.3-full/scr,你也许需要下载相应的php版本安装包。
2、解压php源码。tar -zxf php-5.6.22.tar.gz
3、进入php源码中的ext/mssql目录。cd php-5.6.22/ext/mssql
4、执行phpize命令(本机正在运行的php目录下的phpize)/usr/local/php/bin/phpize
5、开始设置配置文件,红色部分是你真实的路径:./configure –with-php-config=/usr/local/php/bin/php-config –with-mssql=/usr/local/freetds
6、编译:make
7、编译安装:make install
8、打开你的php.ini,加入extension=mssql.so这一行。(位置随意,我一般都紧挨着;extension=XXX.dll后天面添加)
9、重启php ,军哥lnmp直接就执行lnmp restart即可。phpinfo可以看到扩展
PS:如果你想直接用unixODBC进行php的mssql odbc连接,可以接着编译pdo_odbc模块。否则直接跳过进入第17步开始看,编译安装pdo_dblib模块。建议不用odbc连接,据说速度慢。
10、安装pdo_odbc扩展:进入php源码中的ext/pdo_odbc目录。cd php-5.6.22/ext/pdo_odbc
11、执行phpize命令:/usr/local/php/bin/phpize
12、执行编译配置,注意最后两个参数用,号分割:./configure –with-php-config=/usr/local/php/bin/php-config –with-pdo-odbc=unixODBC,/usr
13、make && make install
ps:可能出现的错误:
ODBC header file ‘/usr/local/incl/sqlext.h’ not found!
这时你先看是否有这样的提示:
1 2 3 |
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers. 如果有,安装re2c即可消除not found错误,否则直接运行 <span style="color: #ff0000;">yum -y install unixODBC-devel</span>即可。 安装re2c: |
A、wget -c https://sourceforge.net/projects/re2c/files/0.16/re2c-0.16.tar.gz
B、tar zxf re2c-0.16.tar.gz && cd re2c-0.16
C、./configure
D、make && make install
安装完re2c后,再make就不会报错了。
14、修改php.ini,增加:extension=pdo_odbc.so
15、重启php.
16、pdo_odbc连接的示例代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[php] error_reporting(E_ALL); ini_set('display_errors', '1'); //将出错信息输出到一个文本文件 ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); $dsn = 'odbc:DSN=&amp;amp;amp;lt;span style="color: #ff0000;"&amp;amp;amp;gt;my_mssql&amp;amp;amp;lt;/span&amp;amp;amp;gt;;UID=你的用户名;PWD=你的密码'; $user = '你的用户名'; $pass = '你的密码'; $c = new PDO($dsn, $user, $pass); $query = 'select top 5 * from 你的表名'; $stmt = $c-&amp;amp;amp;amp;gt;query( $query ); while ( $row = $stmt-&amp;amp;amp;amp;gt;fetch( PDO::FETCH_ASSOC ) ) { print_r( $row ); } $c = null; [/php] |
17、安装dblib扩展,以便程序可以自由移植。 (windows方式下,推荐使用dblib连接,当然liunx也可以用dblib连接,据说odbc连接方式比较慢。)
18、进入php源码中的ext/pdo_dblib目录。cd php-5.6.22/ext/pdo_dblib
19、执行phpize命令:/usr/local/php/bin/phpize
20、执行配置:./configure –with-php-config=/usr/local/php/bin/php-config –with-pdo-dblib=/usr/local/freetds
31、执行:make && make install
32、修改php.ini
添加:extension=pdo_dblib.so
33、重启php。至此,安装完毕。
php的pdo示例语句:
1 2 3 4 5 6 7 8 9 10 11 |
$pdo = new PDO("dblib:host=数据库服务器IP:端口;dbname=默认数据库",'用户名', '密码'); $sql = sql语句 $stmt = $pdo->prepare($sql);//预执行 $stmt->execute();//执行 $mydatas = $stmt->fetchAll(); //获取结果 foreach($mydatas as $mydata){ //结果集循环 } //释放资源 unset($pdo); unset($stmt); |
PS:环境:centos 6.X + nginx 1.10.0 + php 5.6.22 + sql server 2008
Table 3-1. Versions of the TDS Protocol, by Product
Product | TDS Version | Comment |
---|---|---|
Sybase before System 10, Microsoft SQL Server 6.x | 4.2 | Still works with all products, subject to its limitations. |
Sybase System 10 and above | 5.0 | Still the most current protocol used by Sybase. |
Sybase System SQL Anywhere | 5.0 only | Originally Watcom SQL Server, a completely separate codebase. Our best information is that SQL Anywhere first supported TDS in version 5.5.03 using the OpenServer Gateway (OSG), and native TDS 5.0 support arrived with version 6.0. |
Microsoft SQL Server 7.0 | 7.0 | Includes support for the extended datatypes in SQL Server 7.0 (such as char/varchar fields of more than 255 characters), and support for Unicode. |
Microsoft SQL Server 2000 | 7.1 | Include support for bigint (64 bit integers), variant and collation on all fields. Collation is not widely used. |
Microsoft SQL Server 2005 | 7.2 | Includes support for varchar(max), varbinary(max), xml datatypes and MARS[a]. |
Microsoft SQL Server 2008 | 7.3 | Includes support for time, date, datetime2, datetimeoffset. |
Microsoft SQL Server 2012 or 2014 | 7.4 | Includes support for session recovery. |
N/A | 8.0 | FreeTDS will alias this version to 7.1 for backwards compatibility reasons, but this should be avoided due to future compatibility concerns. See note below on obsolete versions. |
Notes: a. Multiple Active Result Sets. |
For best results, use the highest version of the protocol supported by your server. If you encounter problems, try a lower version. If that works, though, please report it to the mailing list! [1]
TDS 4.2 has limitations
-
ASCII only, of course.
-
RPC is not supported.
-
BCP is not supported.
-
varchar
fields are limited to 255 characters. If your table defines longer fields, they’ll be truncated. -
dynamic queries (also called prepared statements) are not supported.
The protocol version may also affect how database servers interpret commands. For example, Microsoft SQL Server 2000 is known to behave differently with versions 4.2 and 7.0. Version 7.0 is recommended for compatibility with SQL Server tools.
噢!评论已关闭。