今天遇到php连接mssql问题,按照网上各种尝试,最后成功,再此记录下。由于 php 5.3以后就不支持mssql,如果继续用mssql_打头的函数,那是行不通了,要下载相应的The SQL Server Driver for PHP。现在微软官网有四个安装包:SQLSRV20/30/31/32.exe
用于 SQL Server Driver for PHP 的 API 名称是 sqlsrv。所有 sqlsrv 函数都以 sqlsrv_ 打头,后跟动词或名词。后跟动词的函数用于执行特定操作,而后跟名词的函数用于返回特定形式的元数据。
以下是官方对各php driver支持的php版本及对数据库支持的清单:
- Version support for PHP is as follows
- Version 3.2 supports PHP 5.6, 5.5, and 5.4
- Version 3.1 supports PHP 5.5 and 5.4
- Version 3.0 supports PHP 5.4.
- Versions 3.2 and 3.1 of the driver require Microsoft ODBC Driver 11 (or higher). You can download the Microsoft ODBC Driver 11 for SQL Server from the Microsoft® ODBC Driver 11 for SQL Server® - Windows page.
- Version 3.0 requires the x86 version of Microsoft SQL Server 2012 Native Client.
- Version 2.0 requires the x86 version of Microsoft SQL Server 2008 R2 Native Client.
函数 | 说明 |
---|---|
开始事务。 | |
取消语句;并放弃相应语句的所有未决结果。 | |
提供有关客户端的信息。 | |
关闭连接。释放与相应连接关联的所有资源。 | |
提交事务。 | |
更改错误处理和日志记录配置。 | |
创建一个连接,并将其打开。 | |
返回关于上一操作的错误和/或警告信息。 | |
执行预定义语句。 | |
使下一行的数据可供读取。 | |
以数值索引数组、关联数组或这两种数组的形式检索下一行的数据。 | |
以对象形式检索下一行的数据。 | |
返回字段元数据。 | |
关闭语句。释放与相应语句关联的所有资源。 | |
返回指定配置设置的值。 | |
按索引检索当前行中的字段。可以指定 PHP 返回类型。 | |
检测结果集是否具有一行或多行。 | |
使下一结果可供处理。 | |
报告结果集中的行数。 | |
检索活动结果集中的字段数。 | |
准备 Transact-SQL 查询,但不执行该查询。隐式绑定参数。 | |
准备 Transact-SQL 查询,并将其执行。 | |
回滚事务。 | |
返回有所修改的行的数目。 | |
在每次调用函数时向服务器发送最多八千字节 (8 KB) 的数据。 | |
提供有关服务器的信息。 |
$MSSQL_SERVER="localhost";
$connectionInfo = array( "Database"=>"DBNAME", "UID"=>"sa", "PWD"=>"123456");
/*********************/
/* */
/* Version : 5.1.0 */
/* Author : RM */
/* Comment : ms_conn.php */
/* */
/*********************/
//MSSQL连接部分
function open_mssqlconn( )
{
global $mssql_connection;
include_once( "oa_config.php" );
if (!$mssql_connection){
$mssql_link=sqlsrv_connect( $MSSQL_SERVER, $connectionInfo);
if (!$mssql_link){
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
return $mssql_link;
} else {
return $mssql_connection;
}
}
//SQL查询
function mssqlquery($Q)
{
if (!$mssql_connection){
$mssql_connection=open_mssqlconn();
}
$cursor=sqlsrv_query($mssql_connection,$Q);
if ( !$cursor )
{
echo "SQL查询出错.\n";
die( print_r( sqlsrv_errors(), true));
}
return $cursor;
}
//执行SQL
function mssqlexec($Q){
$stmt = sqlsrv_prepare( $mssql_connection, $Q);
if( $stmt===false )
{
echo "SQL预编译出错.\n";
return false;
die( print_r( sqlsrv_errors(), true));
}
/* Execute the statement. Display any errors that occur. */
if( sqlsrv_execute( $stmt)===false)
{
echo "执行SQL出错.\n";
return false;
die( print_r( sqlsrv_errors(), true));
}
/* Free the statement and connection resources. */
sqlsrv_free_stmt($stmt);
return true;
}
if (!$mssql_connection)
{
$mssql_connection=open_mssqlconn();
}
调用写封装好的方法:
//获取存货名称
public function getcinvname(){
$cinvcode=iconv("UTF-8","GBK", $_POST["cinvcode"]);
$query="select cinvname from inventory where cinvcode='{$cinvcode}'";
$cursor=mssqlquery($query);
$cinvname='';
while($ROW=sqlsrv_fetch_array($cursor,SQLSRV_FETCH_ASSOC)){
$cinvname=iconv("GBK","UTF-8",$ROW["cinvname"]);
}
sqlsrv_free_stmt($cursor);
$this->returnajax(0,$cinvname);
}