1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 317663 $ -->
<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
-->
|