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
SQL Max function in a query
Old 01-04-2010, 01:03 PM SQL Max function in a query
Novice Talker

Posts: 7
Name: Lane Parton
Trades: 0
Okay so in my database I have a column for ID.. It's basically whatever the next number is... But how do I make a script that inserts a new user with the next available ID(higher number)?
Currently I have this:
PHP Code:
<?php
$username
=$_POST['Username'];
$password=$_POST['Password'];
mysql_connect("bbb""bb""bb") or die(mysql_error());
mysql_select_db("bbb") or die(mysql_error());
mysql_query("INSERT INTO 'users' VALUES ('$username', '$password', '0', '0')");
echo 
"<br>";
Print 
"Your information has been successfully added to the database.";
?>
That obviously doesn't work.. But i tested one that does work.. But it's just a temporary file obviously.. All i changed was:
PHP Code:
mysql_query("INSERT INTO 'users' VALUES ('2', '$username', '$password', '0', '0')"); 
This just makes a new user with the id '2'.. Instead of 2 i want something that does the Next Number possible. I read Before that it could be the Max function or whatever.. But i don't know how to use it..
XmINiX is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-04-2010, 02:42 PM Re: SQL Max function in a query
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://www.webmaster-talk.com/php-fo...increment.html
__________________
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 offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-04-2010, 05:59 PM Re: SQL Max function in a query
Novice Talker

Posts: 7
Name: Lane Parton
Trades: 0
I don't understand this.. How would i make it select the next id available using my php file?
I read the topic you sent me a ****load of times.. I just plain don't understand out to use it..
Basically what i mean is..
Quote:
mysql_query("INSERT INTO 'users' VALUES ('HOW DO I MAKE IT FIND THE NEXT ID TO USE AND PLACE IT HERE', '$username', '$password', '0', '0')");
XmINiX is offline
Reply With Quote
View Public Profile
 
Old 01-04-2010, 06:32 PM Re: SQL Max function in a query
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
The point is, you can only estimate what the next id will be for a given statement, because 2 queries can fire simultaneously, and you could have the id mixed up.

In enterprise grade db, we don't use an auto-increment in this case, but what is known as a sequence.
You can replicate it easily in mysql too, in fact, but you will need to be careful upon your insert queries in the future if you go that way.

A sequence is just a counter, with a start value and a step increment.
In db like postgresql, when you want an id with a "like auto-increment" behavior, you use them, with a default value as the next sequence number.

In short, the easiest and most sure way to go is to keep a sequence on your own:
Code:
create table sequences (
  seqId int not null auto-increment,
  seqName varchar(100) not null,
  seqStep int default 1,
  seqVal int default 0,
  primary key (seqId)
);
Then, declare your sequences:
Code:
insert into sequences (seqName) values ('members');
And now, next time you want to insert a member, start by getting and reserving the next id:
PHP Code:
function nextVal($seqName=FALSE){
  if(
$seqName===FALSE){
    return 
FALSE;
  }

  
$q="select seqVal+seqStep as nextVal from sequences where seqName='$seqName'";
  
$res=mysql_query($q);
  while(
$o=mysql_fetch_object($res)){
    
$ret=$o->nextVal;
    
mysql_query("udpate sequences set seqVal=$ret where seqName='$seqName'");
    return 
$ret;
  }

Now, a call to the php nextVal() function will return you the next id to use, and increment the counter value, effectively reserving it.

Now, you only have to use that value when you insert your users profile.
This way, you know you won't have clashes between simultaneous requests, and you will know the id that the user will get before creating his profile.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-04-2010, 06:32 PM Re: SQL Max function in a query
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You do realise that making the ID field to autoincrement it will create the ID in sequence far easier than you trying to handle it.

but to get the next id it is "SELECT MAX(id) AS nextid FROM table;" call that query and pass the value to a variable.
__________________
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 offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-04-2010, 07:35 PM Re: SQL Max function in a query
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
You do realise that making the ID field to autoincrement it will create the ID in sequence far easier than you trying to handle it.
I'm talking about control here.
There are some time I like to know in advance what would be used.
And as mysql lack this basic thing, one day I went and implemented it that way.
It served me well.

Quote:
but to get the next id it is "SELECT MAX(id) AS nextid FROM table;" call that query and pass the value to a variable.
Yep, of course.
But as a dba, you should know that we do care about that 0.001% of chance of a collision between 2 requests.
I don't like deadlocks....

Call me a control freak. In my db's, I am. In life, I just don't really care.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-05-2010, 04:56 AM Re: SQL Max function in a query
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Ok I'll give away a secret I won't use autoincrement in MySql or Identity columns in MSSQL

For one; as you say, it's about control. If you know about and understand the "risk" of collision and how to avoid it in the cases where it may become an issue, it fails to be a consideration.
Secondly; is cross server compatibility. By NOT using "special" features of any DB server, the base code becomes far more flexible and portable.
I don't have to make any allowances for different platforms (or few and far between at least).

Problem is, and not to put too fine a point on it, very often the people asking these questions do not know the difference between a database and a server,
__________________
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 offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 01-05-2010, 06:10 AM Re: SQL Max function in a query
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Ok I'll give away a secret I won't use autoincrement in MySql or Identity columns in MSSQL
I have to say, that doesn't surprise me. Not a bit.
Quote:
For one; as you say, it's about control. If you know about and understand the "risk" of collision and how to avoid it in the cases where it may become an issue, it fails to be a consideration.
As they says, rules are there to be broken.
But before broking them, you have to fully understand why they where put there in the first place.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to SQL Max function in a query
 

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 0.27709 seconds with 12 queries