Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 12-17-2009, 05:22 PM Auto increment
Super Talker

Posts: 128
Name: Jose daSilva
Trades: 0
Hi

My Id in sql db is set to be auto increment. Of course it shows the last one if I query a select file from whatever.

But I would like to see the next id value to appear in a form that I'm filling up, BEFORE saving.

In another words:

Ref: (value of the next value ID)
Country:bala..bla...
and so on...


I did this but does not work:

$ref="<b>".$regist["id"].+1"</b>";

Thanks

Last edited by josil; 12-17-2009 at 05:24 PM..
josil is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-17-2009, 05:36 PM Re: Auto increment
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
SELECT MAX(idcolumn) +1 as nextid FROM table ORDER BY id ASC LIMIT 1;

OR
for MySql V 5.1.12 + you can use
SELECT LAST_INSERT_ID(idcolumn + 1) AS nextid FROM table;

Bear in mind if more than one process or user is inserting records this may not be be accurate as the auto increment on fires on the INSERT process.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-17-2009, 05:46 PM Re: Auto increment
Super Talker

Posts: 128
Name: Jose daSilva
Trades: 0
Quote:
Originally Posted by chrishirst View Post
SELECT MAX(idcolumn) +1 as nextid FROM table ORDER BY id ASC LIMIT 1;

OR
for MySql V 5.1.12 + you can use
SELECT LAST_INSERT_ID(idcolumn + 1) AS nextid FROM table;

Bear in mind if more than one process or user is inserting records this may not be be accurate as the auto increment on fires on the INSERT process.
Thanks

That's the point. Bacause it will be adding quaite a few records by diferente users at the same time. At the moment a lot of ref are recorded in duplicate and triplicate due the fact one user do not know when another is adding records.

I just did this way, but no success:


<? $sql2="SELECT LAST_INSERT_ID(id + 1) AS nextid FROM table";
$result2=mysql_db_query("database",$sql2);
$regist2=mysql_fetch_array($result2);
$ref=$regist2["id"];
?>
<? echo"$ref"; ?>

Last edited by josil; 12-17-2009 at 06:26 PM..
josil is offline
Reply With Quote
View Public Profile
 
Old 12-17-2009, 07:29 PM Re: Auto increment
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
The idea of using an autoincrement numeric column for the ID is to avoid duplications on multi-user systems. The server will assign the next ID as soon as the insert query fires and the auto increment index is updated before a subsequent id number is issued to the next queued query.
By taken the process away from the server you actually INCREASE the risk of duplication and prevent the server from optimising the queries

Why do you need to show the next id to the user before they add a record anyway? It is just as simple to return the ID to the users AFTER their record has been added.

You simply send the insert query and immediately fire a query for the LAST_USED_ID() like so;

INSERT INTO table ..... ;SELECT LAST_INSERT_ID() AS nextid FROM table";

Or if you are using INNODB as the storage engine, you could use transactions.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-17-2009, 07:45 PM Re: Auto increment
Super Talker

Posts: 128
Name: Jose daSilva
Trades: 0
Quote:
Originally Posted by chrishirst View Post
The idea of using an autoincrement numeric column for the ID is to avoid duplications on multi-user systems. The server will assign the next ID as soon as the insert query fires and the auto increment index is updated before a subsequent id number is issued to the next queued query.
By taken the process away from the server you actually INCREASE the risk of duplication and prevent the server from optimising the queries

Why do you need to show the next id to the user before they add a record anyway? It is just as simple to return the ID to the users AFTER their record has been added.

You simply send the insert query and immediately fire a query for the LAST_USED_ID() like so;

INSERT INTO table ..... ;SELECT LAST_INSERT_ID() AS nextid FROM table";

Or if you are using INNODB as the storage engine, you could use transactions.

Let me try that and I will see. Thanks a lot.
josil is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Auto increment
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.05374 seconds with 12 queries