php-doc-en/reference/mysqli/functions/mysqli-prepare.xml
Mehdi Achour 5b71e6af80 ref.mysqli: switch to new style
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@228275 c90b9560-bf6c-de11-be94-00142212c4b1
2007-01-28 04:25:58 +00:00

223 lines
6.1 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.13 $ -->
<refentry id="function.mysqli-prepare">
<refnamediv>
<refname>mysqli_prepare</refname>
<refname>mysqli->prepare()</refname>
<refpurpose>Prepare a SQL statement for execution</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<para>Procedure style:</para>
<methodsynopsis>
<type>mysqli_stmt</type><methodname>mysqli_prepare</methodname>
<methodparam><type>mysqli</type><parameter>link</parameter></methodparam>
<methodparam><type>string</type><parameter>query</parameter></methodparam>
</methodsynopsis>
<para>Object oriented style (method)</para>
<classsynopsis>
<ooclass><classname>mysqli</classname></ooclass>
<methodsynopsis>
<type>mysqli_stmt</type><methodname>prepare</methodname>
<methodparam><type>string</type><parameter>query</parameter></methodparam>
</methodsynopsis>
</classsynopsis>
<para>
Prepares the SQL query pointed to by the null-terminated string query, and
returns a statement handle to be used for further operations on the
statement. The query must consist of a single SQL statement.
</para>
<para>
The parameter markers must be bound to application variables using
<function>mysqli_stmt_bind_param</function> and/or
<function>mysqli_stmt_bind_result</function> before executing the
statement or fetching rows.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
&mysqli.link.description;
<varlistentry>
<term><parameter>query</parameter></term>
<listitem>
<para>
The query, as a string.
</para>
<note>
<para>
You should not add a terminating semicolon or <literal>\g</literal>
to the statement.
</para>
</note>
<para>
This parameter can include one or more parameter markers in the SQL
statement by embedding question mark (<literal>?</literal>) characters
at the appropriate positions.
</para>
<note>
<para>
The markers are legal only in certain places in SQL statements.
For example, they are allowed in the <literal>VALUES()</literal>
list of an <literal>INSERT</literal> statement (to specify column
values for a row), or in a comparison with a column in a
<literal>WHERE</literal> clause to specify a comparison value.
</para>
<para>
However, they are not allowed for identifiers (such as table or
column names), in the select list that names the columns to be
returned by a <literal>SELECT</literal> statement, or to specify both
operands of a binary operator such as the <literal>=</literal> equal
sign. The latter restriction is necessary because it would be
impossible to determine the parameter type. It's not allowed to
compare marker with <literal>NULL</literal> by
<literal>? IS NULL</literal> too. In general, parameters are legal
only in Data Manipulation Languange (DML) statements, and not in Data
Defination Language (DDL) statements.
</para>
</note>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
<function>mysqli_prepare</function> returns a statement object or &false; if an error occured.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Object oriented style</title>
<programlisting role="php">
<![CDATA[
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$city = "Amersfoort";
/* create a prepared statement */
if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {
/* bind parameters for markers */
$stmt->bind_param("s", $city);
/* execute query */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($district);
/* fetch value */
$stmt->fetch();
printf("%s is in district %s\n", $city, $district);
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
?>
]]>
</programlisting>
</example>
<example>
<title>Procedural style</title>
<programlisting role="php">
<![CDATA[
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$city = "Amersfoort";
/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "SELECT District FROM City WHERE Name=?")) {
/* bind parameters for markers */
mysqli_stmt_bind_param($stmt, "s", $city);
/* execute query */
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $district);
/* fetch value */
mysqli_stmt_fetch($stmt);
printf("%s is in district %s\n", $city, $district);
/* close statement */
mysqli_stmt_close($stmt);
}
/* close connection */
mysqli_close($link);
?>
]]>
</programlisting>
</example>
&example.outputs;
<screen>
<![CDATA[
Amersfoort is in district Utrecht
]]>
</screen>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>mysqli_stmt_execute</function></member>
<member><function>mysqli_stmt_fetch</function></member>
<member><function>mysqli_stmt_bind_param</function></member>
<member><function>mysqli_stmt_bind_result</function></member>
<member><function>mysqli_stmt_close</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:"../../../../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
-->