note 64809 added to function.mysqli-insert-id
| From: | will at phpfever dot com | Date: | Fri, 21 Apr 2006 01:40:16 +0000 |
| Subject: | note 64809 added to function.mysqli-insert-id | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-108879@lists.php.net to get a copy of this message | ||
I have received many statements that the insert_id property has a bug because it "works
sometimes". Keep in mind that when using the OOP approach, the actual instantiation of the
mysqli class will hold the insert_id.
The following code will return nothing.
<?php
$mysqli = new mysqli('host','user','pass','db');
if ($result = $mysqli->query("INSERT INTO t (field) VALUES ('value');")) {
echo 'The ID is: '.$result->insert_id;
}
?>
This is because the insert_id property doesn't belong to the result, but rather the actual
mysqli class. This would work:
<?php
$mysqli = new mysqli('host','user','pass','db');
if ($result = $mysqli->query("INSERT INTO t (field) VALUES ('value');")) {
echo 'The ID is: '.$mysqli->insert_id;
}
?>
----
Server IP: 216.194.113.175
Probable Submitter: 12.219.153.176
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.mysqli-insert-id.php
Edit -- http://master.php.net/note/edit/64809
Del: integrated -- http://master.php.net/note/delete/64809/integrated
Del: useless -- http://master.php.net/note/delete/64809/useless
Del: bad code -- http://master.php.net/note/delete/64809/bad+code
Del: spam -- http://master.php.net/note/delete/64809/spam
Del: non-english -- http://master.php.net/note/delete/64809/non-english
Del: in docs -- http://master.php.net/note/delete/64809/in+docs
Del: other reasons-- http://master.php.net/note/delete/64809
Reject -- http://master.php.net/note/reject/64809
Search -- http://master.php.net/manage/user-notes.php