Jump to content

store the value of next auto_increment into php variable using last_insert_id()


Recommended Posts

Hi, I need to find out what the next auto increment ID is going to be when I make a submission to my database via a form because I am uploading images too and they are being stored in  new folder that is made and named the same as the id so that i can link them up when displaying the data. I have this line of code at the moment to store the value of last_insert_id() into the variable $next_id but it tells me it is undefined?

<?php
$query = "SELECT * FROM classifieds (last_insert_id(), '$next_id')"; 

	echo "the next ID to be inserted is $next_id";
	?>

 

Thanks!

<?php
      // insert statement here
      $next_id = mysql_insert_id();
      
      echo "the next ID to be inserted is $next_id";
?>

 

Reading up on the last_insert_id it is suggested not to use it, but if you must, I think this would work:

$query = "SELECT last_insert_id(colname) as next_id FROM classifieds";
$next_id = mysql_fetch_assoc(mysql_query($query));
$next_id = $next_id['next_id'];
echo "the next ID to be inserted is $next_id";

thanks for taking the time on this everyone.

 

I am getting this warning:

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

 

and it is not printing the variable

thanks for taking the time on this everyone.

 

I am getting this warning:

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

 

and it is not printing the variable

 

Post the code you are using, or even read up on the last_insert_id, there are alot of articles that explain it's usage etc. Also check that your mysql version is at least 4.1 or greater or else you are just barking up the wrong tree here.

Is there a better way to go about what I am trying to achieve? For example: query the database for the highest value ID and then add one on and name my image folder that?

 

Won't work eecause

1. ID Increment value is not always 1 (depends on MySQL server configuration)

2. If two or more users are doing it in the same time, they might get same ID

 

Try doing it like this:

 

1. Upload a file to a temporary folder

2. Insert data into database

3. Get last insert ID

4. Create a folder and move file from temporary folder.

 

(pretty much rhodesa already told you that)

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.