Home > php教程 > php手册 > body text

解决php5.3不能连接mssql数据库问题

WBOY
Release: 2016-05-25 16:37:46
Original
1188 people have browsed it

自从php5.3之后系统就不支持mssql_connect这个函数连接了,以前我也讲可以使用com接口来实现,现在我再介绍解决php5.3不能连接mssql数据库另一方法.

windows系统下,PHP5.3以上的版本已经不支持mssql扩展.

首先http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx 点击 get it 下载SQLSRV20.EXE。

将文件解压到php的扩展文件夹ext下,打开php.ini在结尾添加:

[PHP_PDO_SQLSRV] 

extension=php_pdo_sqlsrv_53_nts_vc6.dll 

[PHP_SQLSRV] 

extension=php_sqlsrv_53_nts_vc6.dll 

保存后重启apache即可,附上简单的php连接的例子,代码如下:

<?php
$serverName = "(127.0.0.1)";
$connectionInfo = array(
    "UID" => "root",
    "PWD" => "root2010",
    "Database" => "master"
);
$conn = sqlsrv_connect($serverName, $connectionInfo);
//开源代码phprm.com
if ($conn) {
    echo "Connection established.n";
} else {
    echo "Connection could not be established.n";
    die(print_r(sqlsrv_errors() , true));
}
Copy after login

我使用的是wamp5.1集成安装包,在windows server 2008上面做的测试,php5.4以上版本测试没有成功.

如果使用这个扩展连接Sql server 2005以上版本的sql server(如sql server 2008),你还需要在机器上先安装 SQL Server Native Client :http://download.microsoft.com/download/0/E/6/0E67502A-22B4-4C47-92D3-0D223F117190/sqlncli.msi

这个扩展为php新增了一系列sqlsrv_开头的函数,函数参考如下:

sqlsrv_begin_transaction 
sqlsrv_cancel 
sqlsrv_client_info 
sqlsrv_close 
sqlsrv_commit 
sqlsrv_configure 
sqlsrv_connect 
sqlsrv_errors 
sqlsrv_execute 
sqlsrv_fetch 
sqlsrv_fetch_array 
sqlsrv_fetch_object 
sqlsrv_fetch_metadata 
sqlsrv_free_stmt 
sqlsrv_get_config 
sqlsrv_get_field 
sqlsrv_has_rows 
sqlsrv_next_result 
sqlsrv_num_fields 
sqlsrv_num_rows 
sqlsrv_prepare 
sqlsrv_query 
sqlsrv_rollback 
sqlsrv_rows_affected 
sqlsrv_send_stream_data 
sqlsrv_server_info
Copy after login

更多详细的说明可以在刚才的自解压的文件中有个 SQLServerDriverForPHP.chm 帮助文件中可以找到打开后点击API Reference节点.

另外看一种odb连接方式,代码如下:

$dbhost = "; 
$dbuser = "; //你的mssql用户名 
$dbpass = "; //你的mssql密码 
$dbname = "; //你的mssql库名 
$connect=odbc_connect("Driver={SQL Server};Server=$dbhost;Database=$dbname","$dbuser","$dbpass"); 
$sql="select * from content"; 
$exec=odbc_exec($connect,$sql); 
while($row = (odbc_fetch_array($exec))) 
{ 
$row[&#39;id&#39;]   //?取字段值 
… 
}
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template