php-doc-en/reference/mysqli/functions/mysqli-prepare.xml
Jakub Vrana bd9dfa2fbf Fix return types
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@189159 c90b9560-bf6c-de11-be94-00142212c4b1
2005-06-24 13:46:14 +00:00

198 lines
5.7 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.11 $ -->
<refentry id="function.mysqli-prepare">
<refnamediv>
<refname>mysqli_prepare</refname>
<refname>mysqli->prepare</refname>
<refpurpose>
Prepare a SQL statement for execution
</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<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>
<function>mysqli_prepare</function> 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>
<note>
<para>
You should not add a terminating semicolon or <literal>\g</literal>
to the statement.
</para>
</note>
<para>
The parameter <parameter>query</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 VALUES() list of an INSERT statement
(to specify column values for a row), or in a comparison with a column in
a WHERE 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 SELECT 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>
<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>
&reftitle.returnvalues;
<para>
<function>mysqli_prepare</function> returns a statement object or &false; if an error occured.
</para>
</refsect1>
<refsect1>
&reftitle.seealso;
<para>
<function>mysqli_stmt_execute</function>,
<function>mysqli_stmt_fetch</function>,
<function>mysqli_stmt_bind_param</function>,
<function>mysqli_stmt_bind_result</function>&listendand;
<function>mysqli_stmt_close</function>.
</para>
</refsect1>
<refsect1>
&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>
</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
-->