PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




mysql_insert_id

Name

mysql_insert_id — Gets the AUTO_INCREMENT value (if any) generated by the last query.

Synopsis

int mysql_insert_id([connection]);
mysql link connection (optional): Connection handle returned by mysql_connect() or mysql_pconnect()

Returns

Integer

Description

mysql_insert_id() returns the AUTO_INCREMENT value (if any) generated by the last query made by the script calling mysql_insert_id() . If the connection() argument is not set, the last connection opened is used by default.

Note

mysql_insert_id() only returns a value if the last query on the specified connection handle caused an AUTO_INCREMENT column to generate a value. If you want to get the last AUTO_INCREMENT value generated, use the MySQL function LAST_INSERT_ID() . For example:

echo @ mysql_result (mysql_query ('SELECT LAST_INSERT_ID()'));

Note

mysql_insert_id() also returns the value for the last usage of 'LAST_INSERT_ID(number)'. For example, you can emulate sequences by doing this: 'UPDATE sequence SET id=LAST_INSERT_ID(id+1)' (For more information, see the MySQL manual.)



Caution

PHP converts AUTO_INCREMENT values to longs. If you're using an AUTO_INCREMENT column of type BIGINT, use the MySQL function LAST_INSERT_ID() to get the accurate AUTO_INCREMENT value.

In all cases except when using BIGINT, LAST_INSERT_ID() should return the same value as mysql_insert_id() .



Version

PHP 3+, PHP 4+

Example

Example 817. Get the AUTO_INCREMENT ID generated by the last query

<?php
// Included code that connects to a MySQL server and sets a default database
// See the MySQL Functions chapter introduction for the source code for the file
include ('mysql_connect.inc.php');

// A SELECT query using a column name and a few functions
$query = "INSERT INTO user (login, password) VALUES ('jim', password('beammeupscotty'))";

// Run the query
$mysql_result = @ mysql_query ($query)
    or die ("The query failed, but I shouldn't show you the error message - "
       . "it would give away the user's password.");

// Check whether a value was generated by an AUTO_INCREMENT column in the last query
$last_id = mysql_insert_id ();

if ($last_id) {
   echo "The last query generated an AUTO_INCREMENT value of $last_id";
} else {
   echo "The last query did not generate an AUTO_INCREMENT value";
}
?>



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.