<?xml version="1.0" encoding="utf-8"?> <!-- $Revision$ --> <refentry xml:id="function.mysql-insert-id" xmlns="http://docbook.org/ns/docbook"> <refnamediv> <refname>mysql_insert_id</refname> <refpurpose>Get the ID generated in the last query</refpurpose> </refnamediv> <refsynopsisdiv> <warning> &mysql.alternative.note; <simplelist role="alternatives"> <member><function>mysqli_insert_id</function></member> <member><methodname>PDO::lastInsertId</methodname></member> </simplelist> </warning> </refsynopsisdiv> <refsect1 role="description"> &reftitle.description; <methodsynopsis> <type>int</type><methodname>mysql_insert_id</methodname> <methodparam choice="opt"><type>resource</type><parameter>link_identifier</parameter><initializer>NULL</initializer></methodparam> </methodsynopsis> <para> Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT). </para> </refsect1> <refsect1 role="parameters"> &reftitle.parameters; <para> <variablelist> &mysql.linkid.description; </variablelist> </para> </refsect1> <refsect1 role="returnvalues"> &reftitle.returnvalues; <para> The ID generated for an AUTO_INCREMENT column by the previous query on success, <literal>0</literal> if the previous query does not generate an AUTO_INCREMENT value, or &false; if no MySQL connection was established. </para> </refsect1> <refsect1 role="examples"> &reftitle.examples; <para> <example> <title><function>mysql_insert_id</function> example</title> <programlisting role="php"> <![CDATA[ <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db('mydb'); mysql_query("INSERT INTO mytable (product) values ('kossu')"); printf("Last inserted record has id %d\n", mysql_insert_id()); ?> ]]> </programlisting> </example> </para> </refsect1> <refsect1 role="notes"> &reftitle.notes; <caution> <para> <function>mysql_insert_id</function> will convert the return type of the native MySQL C API function <literal>mysql_insert_id()</literal> to a type of <literal>long</literal> (named <type>int</type> in PHP). If your AUTO_INCREMENT column has a column type of BIGINT (64 bits) the conversion may result in an incorrect value. Instead, use the internal MySQL SQL function LAST_INSERT_ID() in an SQL query. For more information about PHP's maximum integer values, please see the <link linkend="language.types.integer">integer</link> documentation. </para> </caution> <note> <para> Because <function>mysql_insert_id</function> acts on the last performed query, be sure to call <function>mysql_insert_id</function> immediately after the query that generates the value. </para> </note> <note> <para> The value of the MySQL SQL function <literal>LAST_INSERT_ID()</literal> always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries. </para> </note> </refsect1> <refsect1 role="seealso"> &reftitle.seealso; <para> <simplelist> <member><function>mysql_query</function></member> <member><function>mysql_info</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 -->