<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mysqli.insert-id" xmlns="http://docbook.org/ns/docbook">
 <refnamediv>
  <refname>mysqli::$insert_id</refname>
  <refname>mysqli_insert_id</refname>
  <refpurpose>Returns the value generated for an AUTO_INCREMENT column by the last query</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <para>&style.oop;</para>
  <fieldsynopsis><type class="union"><type>int</type><type>string</type></type><varname linkend="mysqli.insert-id">mysqli-&gt;insert_id</varname></fieldsynopsis>
  <para>&style.procedural;</para>
  <methodsynopsis>
   <type class="union"><type>int</type><type>string</type></type><methodname>mysqli_insert_id</methodname>
   <methodparam><type>mysqli</type><parameter>mysql</parameter></methodparam>
  </methodsynopsis>
  <para>
   Returns the ID generated by an <literal>INSERT</literal> or
   <literal>UPDATE</literal> query on a table with a column having the
   <literal>AUTO_INCREMENT</literal> attribute. In the case of a multiple-row
   <literal>INSERT</literal> statement, it returns the first automatically
   generated value that was successfully inserted.
  </para>
  <para>
   Performing an <literal>INSERT</literal> or <literal>UPDATE</literal>
   statement using the <literal>LAST_INSERT_ID()</literal>
   MySQL function will also modify the value returned by <function>mysqli_insert_id</function>.
   If <literal>LAST_INSERT_ID(expr)</literal> was used to generate the value of
   <literal>AUTO_INCREMENT</literal>, it returns the value of the last <literal>expr</literal>
   instead of the generated <literal>AUTO_INCREMENT</literal> value.
  </para>
  <para>
   Returns <literal>0</literal> if the previous statement did not change an 
   <literal>AUTO_INCREMENT</literal> value. <function>mysqli_insert_id</function> must be
   called immediately after the statement that generated the value.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    &mysqli.link.description;
   </variablelist>
  </para>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   The value of the <literal>AUTO_INCREMENT</literal> field that was updated
   by the previous query. Returns zero if there was no previous query on the
   connection or if the query did not update an <literal>AUTO_INCREMENT</literal>
   value.
  </para>
  <para>
   Only statements issued using the current connection affect the return value. The
   value is not affected by statements issued using other connections or clients.
  </para>
  <note>
   <para>
    If the number is greater than the maximum int value, it will be returned as
    a string.
   </para>
  </note>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <example>
   <title><varname>$mysqli-&gt;insert_id</varname> example</title>
   <para>&style.oop;</para>
   <programlisting role="php">
<![CDATA[
<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$mysqli->query("CREATE TABLE myCity LIKE City");

$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);

printf("New record has ID %d.\n", $mysqli->insert_id);

/* drop table */
$mysqli->query("DROP TABLE myCity");
]]>
   </programlisting>
   <para>&style.procedural;</para>
   <programlisting role="php">
<![CDATA[
<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

mysqli_query($link, "CREATE TABLE myCity LIKE City");

$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
mysqli_query($link, $query);

printf("New record has ID %d.\n", mysqli_insert_id($link));

/* drop table */
mysqli_query($link, "DROP TABLE myCity");
]]>
   </programlisting>
   &examples.outputs;
   <screen>
<![CDATA[
New record has ID 1.
]]>
   </screen>
  </example>
 </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
-->
