دالة mysql_insert_id() في PHP
التعريف والاستخدام
تعود دالة mysql_insert_id() هوة ID التي تم إدخالها في الخطوة السابقة.
إذا لم يولد التحقق السابق هوة AUTO_INCREMENT للـ ID، فإن mysql_insert_id() تعود 0.
Syntax
mysql_insert_id(connection)
Parameter | Description |
---|---|
connection | Optional. Specifies the MySQL connection. If not specified, the last connection is used. |
Description
mysql_insert_id() returns the given connection the AUTO_INCREMENT ID number generated in the previous INSERT query. If not specified connection if not specified, the last opened connection is used.
Tips and Comments
Note:If you need to save this value for later use, make sure to call mysql_insert_id() immediately after the query that generates the value.
Example
<?php $con = mysql_connect("localhost", "hello", "321"); if (!$con) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db("test_db",$con); $sql = "INSERT INTO person VALUES ('Carter','Thomas','Beijing')"; $result = mysql_query($sql,$con); echo "ID of the last inserted record is: " . mysql_insert_id();; mysql_close($con); ?>
Output similar to:
ID of the last inserted record is: 5