Jump to content

Recommended Posts

[code]
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
?>
[/code]

The mysql_insert_id() acts on the last performed query so if you are not doing an insert you are probably best to use something like the following:

[code]
<?php
$sql = "select max(id) as max_id from table";
$result = @mysql_query($sql) or die("<tt>problem with $sql</tt>");
if($result) {
  $last_id = @mysql_fetch_object($result);
  echo $last_id->max_id;
}
?>
[/code]
thorpe, if you checked that link it says this:
Sorry, but the function mysql_last_insert is not in the online manual. Perhaps you misspelled it, or it is a relatively new function that hasn't made it into the online documentation yet. The following are the 20 functions which seem to be closest in spelling to mysql_last_insert (really good matches are in bold). Perhaps you were looking for one of these:

mysql_insert_id
mysql_list_dbs
mysql_list_fields
mysqli_insert_id
mysqli_stmt_init
mysqli_stmt_reset
sdo_list_insert
sqlite_last_insert_rowid
msql_list_dbs
msql_list_fields
mysql_change_user
mysql_data_seek
mysql_list_processes
mysql_list_tables
mysql_stat
mysqli_stmt_bind_result
mysqli_stmt_close
mysqli_stmt_error
pdo_lastinsertid
sqlite_last_error

of which I already knew before I asked the question.
Thanks matto, by the way.
Ted
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.