Updating mysqli: insert_id (#627)

This commit is contained in:
Kamil Tekiela 2021-08-05 21:41:10 +01:00 committed by GitHub
parent 6f11457f11
commit 05ccabd6b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,7 +4,7 @@
<refnamediv>
<refname>mysqli::$insert_id</refname>
<refname>mysqli_insert_id</refname>
<refpurpose>Returns the auto generated id used in the latest query</refpurpose>
<refpurpose>Returns the value generated for an AUTO_INCREMENT column by the last query</refpurpose>
</refnamediv>
<refsect1 role="description">
@ -17,19 +17,25 @@
<methodparam><type>mysqli</type><parameter>mysql</parameter></methodparam>
</methodsynopsis>
<para>
The <function>mysqli_insert_id</function> function returns the ID generated
by a query (usually INSERT) on a table with a column having the
AUTO_INCREMENT attribute. If no INSERT or UPDATE statements were sent via this
connection, or if the modified table does not have a column
with the AUTO_INCREMENT attribute, this function will return zero.
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. <methodname>mysqli_insert_id</methodname> must be
called immediately after the statement that generated the value.
</para>
<note>
<para>
Performing an INSERT or UPDATE statement using the LAST_INSERT_ID()
function will also modify the value returned by the
<function>mysqli_insert_id</function> function.
</para>
</note>
</refsect1>
<refsect1 role="parameters">
@ -49,10 +55,14 @@ connection, or if the modified table does not have a column
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 maximal int value, <function>mysqli_insert_id</function>
will return a string.
If the number is greater than the maximum int value, it will be returned as
a string.
</para>
</note>
</refsect1>
@ -65,60 +75,44 @@ connection, or if the modified table does not have a column
<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();
}
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);
printf("New record has ID %d.\n", $mysqli->insert_id);
/* drop table */
$mysqli->query("DROP TABLE myCity");
/* close connection */
$mysqli->close();
?>
]]>
</programlisting>
<para>&style.procedural;</para>
<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();
}
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));
printf("New record has ID %d.\n", mysqli_insert_id($link));
/* drop table */
mysqli_query($link, "DROP TABLE myCity");
/* close connection */
mysqli_close($link);
?>
]]>
</programlisting>
&examples.outputs;
<screen>
<![CDATA[
New Record has id 1.
New record has ID 1.
]]>
</screen>
</example>