reference/mysqli/mysqli/insert-id.xml
c85c9d1d4429496b45f03cd5e77e896821b4f7f3
...
...
@@ -4,32 +4,38 @@
4
4
<refnamediv>
5
5
<refname>mysqli::$insert_id</refname>
6
6
<refname>mysqli_insert_id</refname>
7
-
<refpurpose>Returns the auto generated id used in the latest query</refpurpose>
7
+
<refpurpose>Returns the value generated for an AUTO_INCREMENT column by the last query</refpurpose>
8
8
</refnamediv>
9
9

10
10
<refsect1 role="description">
11
11
&reftitle.description;
12
12
<para>&style.oop;</para>
13
-
<fieldsynopsis><type>mixed</type><varname linkend="mysqli.insert-id">mysqli->insert_id</varname></fieldsynopsis>
13
+
<fieldsynopsis><type class="union"><type>int</type><type>string</type></type><varname linkend="mysqli.insert-id">mysqli-&gt;insert_id</varname></fieldsynopsis>
14
14
<para>&style.procedural;</para>
15
15
<methodsynopsis>
16
-
<type>mixed</type><methodname>mysqli_insert_id</methodname>
17
-
<methodparam><type>mysqli</type><parameter>link</parameter></methodparam>
16
+
<type class="union"><type>int</type><type>string</type></type><methodname>mysqli_insert_id</methodname>
17
+
<methodparam><type>mysqli</type><parameter>mysql</parameter></methodparam>
18
18
</methodsynopsis>
19
19
<para>
20
-
The <function>mysqli_insert_id</function> function returns the ID generated
21
-
by a query (usually INSERT) on a table with a column having the
22
-
AUTO_INCREMENT attribute. If no INSERT or UPDATE statements were sent via this
23
-
connection, or if the modified table does not have a column
24
-
with the AUTO_INCREMENT attribute, this function will return zero.
20
+
Returns the ID generated by an <literal>INSERT</literal> or
21
+
<literal>UPDATE</literal> query on a table with a column having the
22
+
<literal>AUTO_INCREMENT</literal> attribute. In the case of a multiple-row
23
+
<literal>INSERT</literal> statement, it returns the first automatically
24
+
generated value that was successfully inserted.
25
+
</para>
26
+
<para>
27
+
Performing an <literal>INSERT</literal> or <literal>UPDATE</literal>
28
+
statement using the <literal>LAST_INSERT_ID()</literal>
29
+
MySQL function will also modify the value returned by <function>mysqli_insert_id</function>.
30
+
If <literal>LAST_INSERT_ID(expr)</literal> was used to generate the value of
31
+
<literal>AUTO_INCREMENT</literal>, it returns the value of the last <literal>expr</literal>
32
+
instead of the generated <literal>AUTO_INCREMENT</literal> value.
33
+
</para>
34
+
<para>
35
+
Returns <literal>0</literal> if the previous statement did not change an
36
+
<literal>AUTO_INCREMENT</literal> value. <function>mysqli_insert_id</function> must be
37
+
called immediately after the statement that generated the value.
25
38
</para>
26
-
<note>
27
-
<para>
28
-
Performing an INSERT or UPDATE statement using the LAST_INSERT_ID()
29
-
function will also modify the value returned by the
30
-
<function>mysqli_insert_id</function> function.
31
-
</para>
32
-
</note>
33
39
</refsect1>
34
40

35
41
<refsect1 role="parameters">
...
...
@@ -49,10 +55,14 @@ connection, or if the modified table does not have a column
49
55
connection or if the query did not update an <literal>AUTO_INCREMENT</literal>
50
56
value.
51
57
</para>
58
+
<para>
59
+
Only statements issued using the current connection affect the return value. The
60
+
value is not affected by statements issued using other connections or clients.
61
+
</para>
52
62
<note>
53
63
<para>
54
-
If the number is greater than maximal int value, <function>mysqli_insert_id</function>
55
-
will return a string.
64
+
If the number is greater than the maximum int value, it will be returned as
65
+
a string.
56
66
</para>
57
67
</note>
58
68
</refsect1>
...
...
@@ -60,72 +70,55 @@ connection, or if the modified table does not have a column
60
70
<refsect1 role="examples">
61
71
&reftitle.examples;
62
72
<example>
63
-
<title><varname>$mysqli->insert_id</varname> example</title>
73
+
<title><varname>$mysqli-&gt;insert_id</varname> example</title>
64
74
<para>&style.oop;</para>
65
75
<programlisting role="php">
66
76
<![CDATA[
67
77
<?php
68
-
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
69
78

70
-
/* check connection */
71
-
if (mysqli_connect_errno()) {
72
-
printf("Connect failed: %s\n", mysqli_connect_error());
73
-
exit();
74
-
}
79
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
80
+
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
75
81

76
82
$mysqli->query("CREATE TABLE myCity LIKE City");
77
83

78
84
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
79
85
$mysqli->query($query);
80
86

81
-
printf ("New Record has id %d.\n", $mysqli->insert_id);
87
+
printf("New record has ID %d.\n", $mysqli->insert_id);
82
88

83
89
/* drop table */
84
90
$mysqli->query("DROP TABLE myCity");
85
-

86
-
/* close connection */
87
-
$mysqli->close();
88
-
?>
89
91
]]>
90
92
</programlisting>
91
93
<para>&style.procedural;</para>
92
94
<programlisting role="php">
93
95
<![CDATA[
94
96
<?php
95
-
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
96
97

97
-
/* check connection */
98
-
if (mysqli_connect_errno()) {
99
-
printf("Connect failed: %s\n", mysqli_connect_error());
100
-
exit();
101
-
}
98
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
99
+
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
102
100

103
101
mysqli_query($link, "CREATE TABLE myCity LIKE City");
104
102

105
103
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
106
104
mysqli_query($link, $query);
107
105

108
-
printf ("New Record has id %d.\n", mysqli_insert_id($link));
106
+
printf("New record has ID %d.\n", mysqli_insert_id($link));
109
107

110
108
/* drop table */
111
109
mysqli_query($link, "DROP TABLE myCity");
112
-

113
-
/* close connection */
114
-
mysqli_close($link);
115
-
?>
116
110
]]>
117
111
</programlisting>
118
112
&examples.outputs;
119
113
<screen>
120
114
<![CDATA[
121
-
New Record has id 1.
115
+
New record has ID 1.
122
116
]]>
123
117
</screen>
124
118
</example>
125
119
</refsect1>
126
120

127
121
</refentry>
128
-

129
122
<!-- Keep this comment at the end of the file
130
123
Local variables:
131
124
mode: sgml
132
125