0

Hey,

I need to get the last ID from my mysql database and add +1 to it.

I saw this mysql_insert_id(); but it looks like it only gets the last insert id? But what happens if the last query was a different table?

Dan

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Danny159
1

If you are setting your id field preferences to be AUTO_INCREMENT , then this solution
will work for you

<?php
$MaxID = ""; //Declare A Variable to hold the max id and set it to nothing
$table = "myTable";  // Declare the table name that you want capture its max id
//1- Set Connection to your database

// 2- Select the maximum id presents in your table which corresponds to your last id (beacuse id is auto incrementing)
$sql = "SELECT MAX(id) FROM {$myTable}";
$query = mysql_query($sql);
$row = mysql_fetch_array($query) OR die(mysql_error());
$MaxID = $row['MAX(id)'];
$MaxID += 1; // Add One to  it
?>

Test it and let me know if u had any problems

Edited by Amr87: n/a

0

sorry I had error in the $sql variable it should be

$sql = "SELECT MAX(id) FROM {$table}";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.