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

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

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

$sql = "SELECT MAX(id) FROM {$table}";

Just provide me the database table. I wil provide you the query

Amr87 got the fix its working now thanks =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.