SQL Server Driver for PHP Version 1.0 and Version 1.1
sqlsrv_query

Prepares and executes a statement.

Cc296184.note(en-US,SQL.90).gifNote:
This function is intended for execution of one-time queries. For more information, see How to: Execute a Single Query.

Syntax

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

$conn: The connection resource associated with the prepared statement.

$tsql: The Transact-SQL expression that corresponds to the prepared statement.

$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.
  • 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 table below:

    Element Description

    $value

    A literal value, a PHP variable, or a PHP by-reference variable.

    $direction[OPTIONAL]

    One of the following SQLSRV_PARAM_* constants used to indicate the parameter direction: SQLSRV_PARAM_IN, SQLSRV_PARAM_OUT, SQLSRV_PARAM_INOUT. The default value is SQLSRV_PARAM_IN.

    For more information about PHP constants, see SQLSRV Constants.

    $phpType[OPTIONAL]

    A SQLSRV_PHPTYPE_* constant that specifies PHP data type of the returned value.

    $sqlType[OPTIONAL]

    A SQLSRV_SQLTYPE_* constant that specifies the SQL Server data type of the input value.

$options [OPTIONAL]: An associative array that sets query properties. The supported keys are as follows:

Key Supported Values Description

QueryTimeout

A positive integer value.

Sets the query timeout in seconds. By default, the driver will wait indefinitely for results.

SendStreamParamsAtExec

true or false

The default value is true.

Configures the driver to send all stream data at execution (true), or to send stream data in chunks (false). By default, the value is set to true. For more information, see sqlsrv_send_stream_data.

Scrollable

SQLSRV_CURSOR_FORWARD

SQLSRV_CURSOR_STATIC

SQLSRV_CURSOR_DYNAMIC

SQLSRV_CURSOR_KEYSET

This key was added in version 1.1 of the SQL Server Driver for PHP.

For more information about these values, see Specifying a Cursor Type and Selecting Rows.

Return Value

A statement resource. If the statement cannot be created and/or executed, false is returned.

Example

The example below updates a field in the Sales.SalesOrderDetail table of the AdventureWorks database. 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.

<?php
/* 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);
?>
See Also

Tasks

How to: Perform Parameterized Queries
How to: Send Data as a Stream

Concepts

About Code Examples in the Documentation

Other Resources

API Reference (SQL Server Driver for PHP)
Design Considerations
Using Directional Parameters

>
Tags :


Page view tracker