|
|
|
|
Thread title: incrementing ids |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
04-21-2007, 09:28 PM
|
#1
|
Status: I love this place
Join date: Jan 2007
Location: Charlotte
Expertise:
Software:
Posts: 542
|
incrementing ids
how would i start a user id when then register at 10001 and then from there every user that registers itll add one.
for example
1st user - id = 10001
2nd user - id = 10002
3rd user - id = 10003
etc......
|
|
04-21-2007, 09:42 PM
|
#2
|
Status: Senior Member
Join date: Jul 2005
Location:
Expertise:
Software:
Posts: 835
|
Set the database to auto_increment & unique, start from 0 in the database, and in the actual script just pull it out and add 10000 to it...
|
|
04-21-2007, 09:46 PM
|
#3
|
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
Posts: 6,894
|
dont use the auto_increment property, just use this code
PHP Code:
$count = mysql_num_rows($query);
$count = $count + 10001;
mysql_query("INSERT INTO table VALUES ('$count'........)");
if you are going to delete rows, use this code
PHP Code:
$query = mysql_query("SELECT id FROM table ORDER BY id DESC LIMIT 1");
$id = mysql_result($query,'0',"id")+1;
mysql_query("INSERT INTO table VALUES ('$id'........)");
With this one you will have to start with one row in the database, id it 10000
|
|
04-21-2007, 09:50 PM
|
#4
|
Status: Go Baby!!
Join date: Jul 2006
Location: Toronto, ON
Expertise:
Software:
Posts: 1,372
|
Originally Posted by Village Idiot
dont use the auto_increment property, just use this code
PHP Code:
$count = mysql_num_rows($query);
$count = $count + 10001;
mysql_query("INSERT INTO table VALUES ('$count'........)");
if you are going to delete rows, use this code
PHP Code:
$query = mysql_query("SELECT id FROM table ORDER BY id DESC LIMIT 1");
$id = mysql_result($query,'0',"id")+1;
mysql_query("INSERT INTO table VALUES ('$id'........)");
With this one you will have to start with one row in the database, id it 10000
|
Wow. This is very close to Turing and Visual Basic (I'm learning both in school.)
|
|
04-21-2007, 10:04 PM
|
#5
|
Status: I love this place
Join date: Jan 2007
Location: Charlotte
Expertise:
Software:
Posts: 542
|
yeah thats how i was going to do it. with the adding 1 to each one.. but i didnt know if there was any easier way of doing it. Thanks.
|
|
04-21-2007, 10:14 PM
|
#6
|
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
Posts: 3,820
|
Originally Posted by Village Idiot
dont use the auto_increment property
|
Why on earth not? Just specify the id when inserting the very first row, and leave it out from then on.
|
|
04-21-2007, 10:17 PM
|
#7
|
Status: Member
Join date: Oct 2005
Location: Clifton Park, NY, USA
Expertise:
Software:
Posts: 273
|
Auto increments are definitely the safest way to handle incrementing values like this.
I believe you can tell MySQL to set the starting value of the auto_increment field:
ALTER TABLE tbl_name AUTO_INCREMENT = 10000;
HTH,
Mubs
|
|
04-24-2007, 02:11 AM
|
#8
|
Status: Member
Join date: Feb 2007
Location:
Expertise:
Software:
Posts: 137
|
I agree with iisbum, and also far far faster.
Village Idiot:
Code:
$count = mysql_num_rows($query);
$count = $count + 10001;
I presume you have a query like:
Code:
SELECT * FROM table
Well, what happens if you have 10,000 rows? **** starts to get slower!
Id recommend using the count parameter and a query alike:
Code:
$query = mysql_query("SELECT count(*) as count FROM table");
$count = mysql_fetch_array($query);
$count = $count['count'];
$count = $count + 10001;
|
|
04-24-2007, 02:51 AM
|
#9
|
Status: Member
Join date: Apr 2007
Location: Deer Park, NY
Expertise:
Software:
Posts: 123
|
Are you talking about the index value in the SQL database because that is how I am understanding your question. If you want your index value to start from 10000 set the default value of that column to 10000. That's how it works for me at least, or you can just create a row with the index value of 10000 delete that row and everything after that auto-increments from 10000.
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|