new in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
<?erick?>
Forum Newbie
Posts: 12
Joined: Thu May 29, 2008 8:36 am

new in php

Post by <?erick?> »

hello guys please help me i want to us some a little bit question.. im new in php development my problem is i want to create increting number and insert to mysql.. just like auto increment but i want to use php code.. and they have condition if the is change the incrementation is back to one.. advance thank you.. please help me... :roll:
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: new in php

Post by onion2k »

Don't do this. It's an incredibly bad idea. It's something that on the face of it is really simple, but to do it properly you'll have to solve loads of problems like locking, race conditions, speed, etc. The people who code databases have spent years tackling those issues, so there's no point redoing it all yourself. Go back to the client and figure out a proper solution.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: new in php

Post by califdon »

onion2k wrote:Don't do this. It's an incredibly bad idea. It's something that on the face of it is really simple, but to do it properly you'll have to solve loads of problems like locking, race conditions, speed, etc. The people who code databases have spent years tackling those issues, so there's no point redoing it all yourself. Go back to the client and figure out a proper solution.
I agree with onion2k. Practical database design is definitely not intuitive. Things that seem logical to "ordinary" people often create substantial problems in databases. As onion2k said, computer scientists have spent decades establishing rules about database design principles. Follow their advice unless you think that you know more about it than they do.
<?erick?>
Forum Newbie
Posts: 12
Joined: Thu May 29, 2008 8:36 am

Re: new in php

Post by <?erick?> »

Thank you for the advice but I forgot to specified my previous question the specification of may question is the number I want to increment number leading seven zeroes or zerofill from left just like this 00000001 to 10000000 and if the current year is increment the number is go back to seven zero + 1. And also if the increment number reach the maximum also to 00000001. All of this is storing to mysql so anyone wants to post code for this question. Help me and thank you again… :idea:
dml
Forum Contributor
Posts: 133
Joined: Sat Jan 26, 2008 2:20 pm

Re: new in php

Post by dml »

If I understand correctly, you're generating ids along the lines of 2008-0000001, 2008-00000002. If the ids run out before the end of the year, you reuse ids.

How about using a MyISAM table with a multi column autoincrement.
For MyISAM and BDB tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column) + 1 WHERE prefix=given-prefix.
As for restricting to seven digits and wrapping, let the database generate a unique id, which is always useful, and take the modulo and format it.
<?erick?>
Forum Newbie
Posts: 12
Joined: Thu May 29, 2008 8:36 am

Re: new in php

Post by <?erick?> »

yes, but im not familliar to myisam.. any Suggestion
dml
Forum Contributor
Posts: 133
Joined: Sat Jan 26, 2008 2:20 pm

Re: new in php

Post by dml »

It's just a type of table in mysql. Try this out:

Code: Select all

 
create table t(y year, id int unsigned not null auto_increment, primary key(y,id)) engine=MyISAM;
insert into t(y) values(2008);
insert into t(y) values(2008);
select * from t;
 
<?erick?>
Forum Newbie
Posts: 12
Joined: Thu May 29, 2008 8:36 am

Re: new in php

Post by <?erick?> »

any php code??for this question? :banghead:
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: new in php

Post by onion2k »

<?erick?> wrote:any php code??for this question? :banghead:
No, because solving this problem with PHP is the wrong way to approach it. It's a data issue, so it should be solved with the database if possible.

Frankly the idea of reusing database keys when you're only storing 100 million records sounds silly anyway. That's a lot of data, but MySQL can cope with a lot more.

@dml: Nice idea. I didn't know you could use multiple columns for an auto increment. That's sweet.
<?erick?>
Forum Newbie
Posts: 12
Joined: Thu May 29, 2008 8:36 am

Re: new in php

Post by <?erick?> »

Thank you for you all suggestion.. :mrgreen:
Post Reply