sqlsrv_querysqlsrv_query

04/11/2019

本文内容

准备并执行语句。Prepares and executes a statement.

语法Syntax

sqlsrv_query(resource $conn, string $tsql [, array $params [, array $options]])

参数Parameters

$conn:与已准备的语句相关联的连接资源。$conn: The connection resource associated with the prepared statement.

$tsql:对应于已准备的语句的 Transact-SQL 表达式。$tsql: The Transact-SQL expression that corresponds to the prepared statement.

$params [可选]:对应于参数化查询中参数的值的阵列 。$params [OPTIONAL]: An array of values that correspond to parameters in a parameterized query. 该阵列的每个元素可以是以下项之一:Each element of the array can be one of the following:

文字值。A literal value.

PHP 变量。A PHP variable.

具有以下结构的 阵列 :An array with the following structure:

array($value [, $direction [, $phpType [, $sqlType]]])

下表提供对阵列的每个元素的描述:The description for each element of the array is in the following table:

元素Element

说明Description

$value$value

参数值、PHP 变量或通过引用传递的 PHP 变量。A literal value, a PHP variable, or a PHP by-reference variable.

$direction[可选]$direction[OPTIONAL]

用于指示参数方向的以下 SQLSRV_PARAM__ 常量之一:SQLSRV_PARAM_IN、SQLSRV_PARAM_OUT、SQLSRV_PARAM_INOUT * 。One of the following **SQLSRV_PARAM**_ constants used to indicate the parameter direction: _* SQLSRV_PARAM_IN**, SQLSRV_PARAM_OUT, SQLSRV_PARAM_INOUT. 默认值为 SQLSRV_PARAM_IN 。The default value is SQLSRV_PARAM_IN.

$phpType[可选]$phpType[OPTIONAL]

*SQLSRV_PHPTYPE__ 常量,用于指定返回的值的 PHP 数据类型 * 。A *SQLSRV_PHPTYPE_* _ constant that specifies PHP data type of the returned value.

$sqlType*[可选]$sqlType*[OPTIONAL]

*SQLSRV_SQLTYPE__ 常量,用于指定输入值的 SQL Server 数据类型 * 。A *SQLSRV_SQLTYPE_* _ constant that specifies the SQL Server data type of the input value.

$options* [可选]:设置查询属性的关联阵列。$options* [OPTIONAL]: An associative array that sets query properties. It is the same list of keys also supported by sqlsrv_prepare.

返回值Return Value

语句资源。A statement resource. 如果无法创建和/或执行语句,将返回 false 。If the statement cannot be created and/or executed, false is returned.

备注Remarks

sqlsrv_query 函数非常适合一次性查询,并且应该是执行查询的默认选项,除非出现特殊情况 。The sqlsrv_query function is well-suited for one-time queries and should be the default choice to execute queries unless special circumstances apply. 此函数提供了一个简化的方法,以便使用最少的代码来执行查询。This function provides a streamlined method to execute a query with a minimum amount of code. sqlsrv_query 函数可用于语句准备和语句执行,还可用于执行参数化查询 。The sqlsrv_query function does both statement preparation and statement execution, and can be used to execute parameterized queries.

示例 1Example 1

在下面的示例中,向 AdventureWorks 数据库的 Sales.SalesOrderDetail 表格中插入单个行。In the following example, a single row is inserted into the Sales.SalesOrderDetail table of the AdventureWorks database. 该示例假定已在本地计算机上安装了 SQL Server 和 AdventureWorks 数据库。The example assumes that SQL Server and the AdventureWorks database are installed on the local computer. 从命令行运行该示例时,所有输出都将写入控制台。All output is written to the console when the example is run from the command line.

备注

尽管下面的示例使用 INSERT 语句来演示将 sqlsrv_query 用于执行一次性语句,但是上述概念适用于所有 Transact-SQL 语句 。Although the following example uses an INSERT statement to demonstrate the use of sqlsrv_query for a one-time statement execution, the concept applies to any Transact-SQL statement.

/* Connect to the local server using Windows Authentication and

specify the AdventureWorks database as the database in use. */

$serverName = "(local)";

$connectionInfo = array("Database"=>"AdventureWorks");

$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn === false) {

echo "Could not connect.\n";

die(print_r(sqlsrv_errors(), true));

}

/* Set up the parameterized query. */

$tsql = "INSERT INTO Sales.SalesOrderDetail

(SalesOrderID,

OrderQty,

ProductID,

SpecialOfferID,

UnitPrice,

UnitPriceDiscount)

VALUES

(?, ?, ?, ?, ?, ?)";

/* Set parameter values. */

$params = array(75123, 5, 741, 1, 818.70, 0.00);

/* Prepare and execute the query. */

$stmt = sqlsrv_query($conn, $tsql, $params);

if ($stmt) {

echo "Row successfully inserted.\n";

} else {

echo "Row insertion failed.\n";

die(print_r(sqlsrv_errors(), true));

}

/* Free statement and connection resources. */

sqlsrv_free_stmt($stmt);

sqlsrv_close($conn);

?>

示例 2Example 2

下面的示例将更新 AdventureWorks 数据库的 Sales.SalesOrderDetail 表格中的字段 。The following example updates a field in the Sales.SalesOrderDetail table of the AdventureWorks database. 该示例假定已在本地计算机上安装了 SQL Server 和 AdventureWorks 数据库。The example assumes that SQL Server and the AdventureWorks database are installed on the local computer. 从命令行运行该示例时,所有输出都将写入控制台。All output is written to the console when the example is run from the command line.

/* Connect to the local server using Windows Authentication and

specify the AdventureWorks database as the database in use. */

$serverName = "(local)";

$connectionInfo = array("Database"=>"AdventureWorks");

$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn === false) {

echo "Could not connect.\n";

die(print_r(sqlsrv_errors(), true));

}

/* Set up the parameterized query. */

$tsql = "UPDATE Sales.SalesOrderDetail

SET OrderQty = (?)

WHERE SalesOrderDetailID = (?)";

/* Assign literal parameter values. */

$params = array(5, 10);

/* Execute the query. */

if (sqlsrv_query($conn, $tsql, $params)) {

echo "Statement executed.\n";

} else {

echo "Error in statement execution.\n";

die(print_r(sqlsrv_errors(), true));

}

/* Free connection resources. */

sqlsrv_close($conn);

?>

备注

当由于 PHP 的浮点数具有有限精确度而将值绑定到十进制或数值列以确保精确度和准确度时,建议将字符串用作输入。It is recommended to use strings as inputs when binding values to a decimal or numeric column to ensure precision and accuracy as PHP has limited precision for floating point numbers. 这同样适用于 bigint 列,尤其是在值超出整数范围的情况下。The same applies to bigint columns, especially when the values are outside the range of an integer.

示例 3Example 3

此代码示例演示如何将十进制值作为输入参数进行绑定。This code sample shows how to bind a decimal value as an input parameter.

$serverName = "(local)";

$connectionInfo = array("Database"=>"YourTestDB");

$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn === false) {

echo "Could not connect.\n";

die(print_r(sqlsrv_errors(), true));

}

// Assume TestTable exists with a decimal field

$input = "9223372036854.80000";

$params = array($input);

$stmt = sqlsrv_query($conn, "INSERT INTO TestTable (DecimalCol) VALUES (?)", $params);

sqlsrv_free_stmt($stmt);

sqlsrv_close($conn);

?>

示例 4Example 4

此代码示例演示如何创建 sql_variant 类型的表并提取插入的数据。This code sample shows how to create a table of sql_variant types and fetch the inserted data.

$server = 'serverName';

$dbName = 'databaseName';

$uid = 'yourUserName';

$pwd = 'yourPassword';

$options = array("Database"=>$dbName, "UID"=>$uid, "PWD"=>$pwd);

$conn = sqlsrv_connect($server, $options);

if($conn === false) {

die(print_r(sqlsrv_errors(), true));

}

$tableName = 'testTable';

$query = "CREATE TABLE $tableName ([c1_int] sql_variant, [c2_varchar] sql_variant)";

$stmt = sqlsrv_query($conn, $query);

if($stmt === false) {

die(print_r(sqlsrv_errors(), true));

}

sqlsrv_free_stmt($stmt);

$query = "INSERT INTO [$tableName] (c1_int, c2_varchar) VALUES (1, 'test_data')";

$stmt = sqlsrv_query($conn, $query);

if($stmt === false) {

die(print_r(sqlsrv_errors(), true));

}

sqlsrv_free_stmt($stmt);

$query = "SELECT * FROM $tableName";

$stmt = sqlsrv_query($conn, $query);

if(sqlsrv_fetch($stmt) === false) {

die(print_r(sqlsrv_errors(), true));

}

$col1 = sqlsrv_get_field($stmt, 0);

echo "First field: $col1 \n";

$col2 = sqlsrv_get_field($stmt, 1);

echo "Second field: $col2 \n";

sqlsrv_free_stmt($stmt);

sqlsrv_close($conn);

?>

预期的输出为:The expected output would be:

First field: 1

Second field: test_data

另请参阅See Also

php sqlsrv_query,sqlsrv_query相关推荐

  1. php 存储过程 sqlsrv_query,php-PHP如何得用sqlsrv函数来调用MSSQL中的存储过程,并返回数据集?...

    我在MS SQL2008R2中写了一个存储过程,过程体中定义了临时表,通过业务逻辑生成 的数据存入临时表中,最后通过查询语句(Select * From xxxx) 返回临时表中的数据. 现在的问题是 ...

  2. 【转发】PHP连接MSSQL数据库案例,PHPWAMP多个PHP版本连接SQL Server数据库

    转发自:http://blog.csdn.net/lccee/article/details/54289076 课前小知识普及:MSSQL和SQL Server是同一个软件,叫法不同而已,MSSQL全 ...

  3. php drive mssql,PHP 连接 MSSQL 2005/2008 以UTF8存取 并让ADODB支持的安装设置

    时间:2009-11-9 11:14 作者:欣然随风 因系统大量使用AJAX,为避免编码转来转去的麻烦,把文件都设为了UTF8.但在操作MSSQL时遇到乱码问题,查阅一番后原来是MSSQL不支持UTF ...

  4. CodeIgniter2.0中sqlserver驱动返回受影响行数问题解决

    最近使用CI写项目时遇到的问题,当使用sqlserve链接操作时 修改和删除返回的受影响行数不对 解决办法如下: 找到ci框架目录中include\database\drivers\sqlsrv\sq ...

  5. php+sqlserver实现分页效果

    找了一些实现的代码,都或多或少有点问题. 主要问题在于: 在进行一页数据查询时的sql语句格式问题, 开始尝试使用limit关键字查询,错误,limit用于mysql: 接着使用ROWNUM.row_ ...

  6. SQL server注入

    一.基础知识 系统数据库 master数据库 master是SQL server最重要的数据库,是整个数据库的核心,用户不能直接修改.里面数据库包括用户的登陆信息.用户所在的组.所有系统的配置选项.服 ...

  7. freetds php mssql 中文乱码,PHP读取mssql json数据中文乱码的解决办法

    PHP及网页使用UTF-8编码,数据库是sql server2008,使用默认编码(936,即GBK编码) 当读取数据库数据时,使用php自带的json_encode()返回到前端,结果中文不显示. ...

  8. 7.1 pdo 宝塔面板php_CentOS 7.6下宝塔面板 PHP7.2安装sqlsrv扩展

    PHP需要的相关环境 (慎用 yum update)yum update yum install php php-pdo php-xml php-pear php-devel re2c gcc-c++ ...

  9. php5.3无法加载mysql数据库模块_PHP_php5.3不能连接mssql数据库的解决方法,本文实例讲述了php5.3不能连接m - phpStudy...

    php5.3不能连接mssql数据库的解决方法 本文实例讲述了php5.3不能连接mssql数据库的解决方法.分享给大家供大家参考.具体分析如下: 自从php5.3之后系统就不支持mssql_conn ...

最新文章

  1. matlab decomposition filters,MATLAB小波去噪求助(附算法和显示图片)!不知自己哪个地方出了问题,求指点! - 信息科学 - 小木虫 - 学术 科研 互动社区...
  2. p和li之间的应用上的区别
  3. 云ERP系统如何进行流程设计
  4. 接口并发如何模仿用户点击率和提交率_洞察| 五大法则揭秘!在抖音如何打造“爆款”?...
  5. GMF 教程 Mindmap 6
  6. 敢问北极熊,路在何方?
  7. 高效的设计可视化UI
  8. reset.css下载
  9. ActiveSync 无法同步解决记录
  10. mysql or 索引失效_MySQL索引失效的几种情况详析
  11. CRM八面体:客户关系管理成功案例1 荷兰皇家航空 KLM Royal Dutch Airlines
  12. 阿里云账号注销踩坑实践记录
  13. 颜色空间(RGB、YUV、YIQ、CMY)
  14. 曹鹏 其言其人 2009-06-15 17:44
  15. [20181031]如何确定db_link的进程号.txt
  16. 30个响应式大背景网页设计欣赏
  17. 从Linux内核角度看中间人攻击(ARP欺骗)并利用Python scapy实现
  18. onedrive电脑手机不同步_关于OneDrive,移动端同步以及显示不及时的问题。
  19. 补天漏洞平台:让更多的白帽子脱离黑产
  20. SpringSecurity:授权

热门文章

  1. 实战:基于 CNN 的验证码破解项目(附代码)
  2. 想学图像分割,强烈建议从这5篇图像分割算法综述
  3. WCF学习笔记(基于REST规则方式)
  4. 任何社区,只要能影响他人成长的人,都可以成为敏捷个人的荣誉会员
  5. Spring系列之Spring常用注解总结
  6. 实战centos6安装zabbix-2.4版(终极版)
  7. 编写更少量的代码:使用apache commons工具类库
  8. Bootstrap:关于bootstrap单页面中多Modal的问题
  9. MySQL 关于毫秒和微秒的处理,MySQL获取毫秒!
  10. java获取get请求返回_Java中处理调用第三方接口(post/get),该如何处理,返回的数据如何处理...