php sqlsrv_query,sqlsrv_query

晏德佑
2023-12-01

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

 类似资料:

相关阅读

相关文章

相关问答