php调用SQL SERVER 2008及以上版本的方法

来源:互联网 发布:知乎怎么做微信销售 编辑:程序博客网 时间:2024/05/22 02:01

       今天遇到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.

以下是官方提供的API函数列表及说明,函数名链接到官网查看demo!列表中粗字体函数是常用的。

函数说明

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_field_metadata

返回字段元数据。

sqlsrv_free_stmt

关闭语句。释放与相应语句关联的所有资源。

sqlsrv_get_config

返回指定配置设置的值。

sqlsrv_get_field

按索引检索当前行中的字段。可以指定 PHP 返回类型。

sqlsrv_has_rows

检测结果集是否具有一行或多行。

sqlsrv_next_result

使下一结果可供处理。

sqlsrv_num_rows

报告结果集中的行数。

sqlsrv_num_fields

检索活动结果集中的字段数。

sqlsrv_prepare

准备 Transact-SQL 查询,但不执行该查询。隐式绑定参数。

sqlsrv_query

准备 Transact-SQL 查询,并将其执行。

sqlsrv_rollback

回滚事务。

sqlsrv_rows_affected

返回有所修改的行的数目。

sqlsrv_send_stream_data

在每次调用函数时向服务器发送最多八千字节 (8 KB) 的数据。

sqlsrv_server_info

提供有关服务器的信息。

我也上一些代码以供参考:
$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;}//执行SQLfunction 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);}





1 0