Jump to content

SQL PHP question


djpic

Recommended Posts

I am inserting a new entry in a mysql database.  I have one field that is an "auto increment."  What I want to do is find what the "auto increment" value for the entry I am about to enter before I enter it.  In other words, ask the database if I was going to insert a new entry what would the "auto increment" value be equaled to.  Please let me know if there is anything in SQL that will allow me to do this.

 

thanks.

Link to comment
Share on other sites

I don't think there is a fool proof way of doing this unless you were to save the auto-increment value after every record save. The problem is that the next auto-increment value will not be MAX(id) + 1 if the last record created has been deleted.

 

Example, you just inserted a record with the ID of 15. The next auto-increment value will be 16. In that case it WOULD be MAX(ID) +1. But, if the record with the ID of 15 is deleted, the next ID would still be 16 even though MAX(ID) + 1 would now equal 15 (assuming there is a record with the ID of 14).

 

I'm curious why you would need to know the ID before the record is created. Might be a different way of approaching the problem that does not require this.

Link to comment
Share on other sites

http://computer-vet.com/weblog/2006/07/05/find-next-autoindex-using-php-and-mysql.html

 

However, as PFMaBiSmAd alluded to you should NOT rely upon this. If two users access the script which pulls this value then both pages will have the same "Next Auto-Increment" value. But if both were to proceed with the record creation, only one would actually get that value and the other would get the subsequent value. I still think this is a proor approach.

Link to comment
Share on other sites

http://us3.php.net/manual/en/function.mysql-insert-id.php

 

That can be used after the insert call to get the insert id.

 

I could be wrong (and I suspect I am), but I think that gets the last ID from the connection passed to it (or assumed).  That means that even if person X inserts into the table, the ID will be correct.  Once again, I could be wrong.  Testing now lol.

 

 

Edit:

 

I ran:

 

<?php

/*

CREATE DATABASE omg;

CREATE TABLE omg (
id INT AUTO_INCREMENT PRIMARY KEY,
lol varchar(255)
);

*/

error_reporting(E_ALL);

mysql_connect('localhost', 'root', 'root');
mysql_select_db('omg');
mysql_query("INSERT INTO omg (lol) VALUES ('php')");
sleep(30);
echo mysql_insert_id();


?>

 

And then in an SQL cmd line window, I inserted 10 or so rows, and it returned the ID of the record inserted in the php script.

 

Not sure if this helps since you asked for the ID before you insert it, but I'm not quite sure why you would need it before.

Link to comment
Share on other sites

Bleh you typed that while I was editting.

 

Yes, I was aware of that as mentioned in my edit, but why would he need that?  Why would he ever, EVER, need that?  If he does need the ID for some reason or other, maybe a subsequent query or something, he doesn't specifically need it before, and I can't think of why else he would need it before.

Link to comment
Share on other sites

Edit: Oh... you went down the locking path.  I've seen servers literally with 20 second queries because of profuse amounts of locks.  When ever there's a lock, each insert or update gets added to waiting list.  What if he has high traffic?

 

luca200, that won't [always] work.  What if someone else has an insert run inbetween running the query and using the value?

 

And, to correct my "Why would he ever, EVER, need that?" from earlier before someone else sticks my shoe in my mouth, as shown by the other thread, I guess there are situations where it could be nice to know ahead of time (although I think using an autoincrementing, hence guessable value for order IDs is a bad idea [although authorization would obviously almost eleminate that problem]).  (I don't personally see an advantage of giving the user the order ID ahead of time since it's not technically ordered at that point, but what ever... lol)

Link to comment
Share on other sites

We should know exactly what he needs...

you're right, locking tables could cause trouble, and most of all, the best way is inserting the row before other things.

 

But strictly speaking, if he absolutely wants to do that, he can

 

Edit: of course he cannot lock the table for showing the id to the customer, as the lock would be lost between two subsequent http calls. In case that was the problem

Link to comment
Share on other sites

Well, I still think this makes no sense trying to determine the value before it is created since it can, and will, be wrong in some instances.

 

How about this: Once a user starts the order process (by adding products to the cart) create a record in the orders table so you can generate an order number. But, have an "isOrder" column and set the value initially to "0" (false). One the order is finalized, set the "isOrder" column to "1" (true). You can then query for that order number any time before the order is completed. The only downside would be that any order not completed would take up an order number record, but they could be easily weeded out using the "isOrder" column.

Link to comment
Share on other sites

lol...so sorry for the late reply.  The reason why I need the new auto_entrement value is because I am using that as an ProductID of sorts.  When the person / admin enters a new product with a photo, the php application will change the file name to: (productid)_(photonumber).jpg  But if the product has not yet been entered, then I do not know the product ID therefore can not create the correct file name.  I guess I could have the script add the product information and then add the entry and refer to another script which will add the photo with the file name and product ID of the new product if that makes any since.  Just thought it would be better to do it all in one form / script though.

 

Again sorry for the late reply, I didn't subscribe to this thread or say notify me so I kind of forgot about it until I started working on the project again.  That is the problem when you are the only person working on 5 projects at a time.

 

Thanks for the help though.

Link to comment
Share on other sites

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.