php-doc-en/reference/sqlsrv/functions/sqlsrv-prepare.xml
Richard Quadling 8b6d169424 Native EOL
Use consistent line terminator.


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@317663 c90b9560-bf6c-de11-be94-00142212c4b1
2011-10-03 16:27:39 +00:00

237 lines
No EOL
7.6 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.sqlsrv-prepare" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>sqlsrv_prepare</refname>
<refpurpose>Prepares a query for execution</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>mixed</type><methodname>sqlsrv_prepare</methodname>
<methodparam><type>resource</type><parameter>conn</parameter></methodparam>
<methodparam><type>string</type><parameter>sql</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>params</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>options</parameter></methodparam>
</methodsynopsis>
<para>
Prepares a query for execution. This function is ideal for preparing a query
that will be executed multiple times with different parameter values.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>conn</parameter></term>
<listitem>
<para>
A connection resource returned by <function>sqlsrv_connect</function>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>sql</parameter></term>
<listitem>
<para>
The string that defines the query to be prepared and executed.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>params</parameter></term>
<listitem>
<para>
An array specifying parameter information when executing a parameterized
query. Array elements can be any of the following:
<simplelist>
<member>A literal value</member>
<member>A PHP variable</member>
<member>An array with this structure:
array($value [, $direction [, $phpType [, $sqlType]]])</member>
</simplelist>
The following table describes the elements in the array structure above:
</para>
<table>
<title>Array structure</title>
<tgroup cols="2">
<thead>
<row>
<entry>Element</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>$value</entry>
<entry>A literal value, a PHP variable, or a PHP by-reference variable.</entry>
</row>
<row>
<entry>$direction (optional)</entry>
<entry>One of the following SQLSRV constants used to indicate the
parameter direction: SQLSRV_PARAM_IN, SQLSRV_PARAM_OUT, SQLSRV_PARAM_INOUT.
The default value is SQLSRV_PARAM_IN. </entry>
</row>
<row>
<entry>$phpType (optional)</entry>
<entry>A SQLSRV_PHPTYPE_* constant that specifies PHP data type of the
returned value.</entry>
</row>
<row>
<entry>$sqlType (optional)</entry>
<entry>A SQLSRV_SQLTYPE_* constant that specifies the SQL Server data
type of the input value.</entry>
</row>
</tbody>
</tgroup>
</table>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>options</parameter></term>
<listitem>
<para>
An array specifing query property options. The supported keys are described
in the following table:
</para>
<table>
<title>Query Options</title>
<tgroup cols="3">
<thead>
<row>
<entry>Key</entry>
<entry>Values</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry>QueryTimeout</entry>
<entry>A positive integer value.</entry>
<entry>Sets the query timeout in seconds. By default, the driver will
wait indefinitely for results.</entry>
</row>
<row>
<entry>SendStreamParamsAtExec</entry>
<entry>&true; or &false; (the default is &true;)</entry>
<entry>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 <function>sqlsrv_send_stream_data</function>.</entry>
</row>
<row>
<entry>Scrollable</entry>
<entry>SQLSRV_CURSOR_FORWARD, SQLSRV_CURSOR_STATIC, SQLSRV_CURSOR_DYNAMIC,
or SQLSRV_CURSOR_KEYSET</entry>
<entry>See <link xlink:href="&url.sqlsrv.specify.cursortype;">Specifying a Cursor Type and Selecting Rows</link>
in the Microsoft SQLSRV documentation.</entry>
</row>
</tbody>
</tgroup>
</table>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns a statement resource on success and &false; if an error occurred.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>sqlsrv_prepare</function> example</title>
<para>
This example demonstrates how to prepare a statement with <function>sqlsrv_prepare</function>
and re-execute it multiple times (with different parameter values) using <function>sqlsrv_execute</function>.
</para>
<programlisting role="php">
<![CDATA[
<?php
$serverName = "serverName\sqlexpress";
$connectionInfo = array( "Database"=>"dbName", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false) {
die( print_r( sqlsrv_errors(), true));
}
$sql = "UPDATE Table_1
SET OrderQty = ?
WHERE SalesOrderID = ?";
// Initialize parameters and prepare the statement.
// Variables $qty and $id are bound to the statement, $stmt.
$qty = 0; $id = 0;
$stmt = sqlsrv_prepare( $conn, $sql, array( &$qty, &$id));
if( !$stmt ) {
die( print_r( sqlsrv_errors(), true));
}
// Set up the SalesOrderDetailID and OrderQty information.
// This array maps the order ID to order quantity in key=>value pairs.
$orders = array( 1=>10, 2=>20, 3=>30);
// Execute the statement for each order.
foreach( $orders as $id => $qty) {
// Because $id and $qty are bound to $stmt1, their updated
// values are used with each execution of the statement.
if( sqlsrv_execute( $stmt ) === false ) {
die( print_r( sqlsrv_errors(), true));
}
}
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<para>
When you prepare a statement that uses variables as parameters, the variables
are bound to the statement. This means that if you update the values of the
variables, the next time you execute the statement it will run with updated
parameter values. For statements that you plan to execute only once, use
<function>sqlsrv_query</function>.
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>sqlsrv_execute</function></member>
<member><function>sqlsrv_query</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->