473,468 Members | 1,358 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

any of you ever wrote a get_next_id function?

function getNewID($fieldName, $tableName) { // INT "METHOD" FOR
NON-AUTO-INCREMENT FIELDS
global $dbConn;
$sql = "SELECT MAX($fieldName) FROM $tableName";
$query = mysql_query($sql, $dbConn) or die('Could not perform query: ' .
mysql_error());
if (!($row = mysql_fetch_row($query))) return 1;
return $row[0];
}

I'm trying to retrieve the max(ID) from a column that cannot be
auto_incremented (since mySQL version on this remote site only allows for
tables to have ONE auto_increment field - how stupid), the basic premise is
this:

I either return
1) the max ID
2) '1'

How have you guys done it, or what have I missed?

Phil
Jul 17 '05 #1
5 2194
Phil Powell wrote:
I either return
1) the max ID
2) '1'

How have you guys done it, or what have I missed?


You have missed what happens when more than one request comes at the
same time.

Just a question - why do you need more than one auto-inc in a single
table ?

--
Spam:newsgroup(at)cr*********@verisign-sux-klj.com
EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>
Jul 17 '05 #2
Hi Phil,

Agreed that simultaneous requests will cause problems, but what
mechanisms for ensuring unique column identifiers are commonly used
outside of MySQL - e.g. SQL Server, Postgres - where auto_inc is not
available?

- TL

127.0.0.1 wrote:
Phil Powell wrote:

I either return
1) the max ID
2) '1'

How have you guys done it, or what have I missed?

You have missed what happens when more than one request comes at the
same time.

Just a question - why do you need more than one auto-inc in a single
table ?


Jul 17 '05 #3
Tom Lee <tl*****@webcrumb.com> schrieb:
Agreed that simultaneous requests will cause problems, but what
mechanisms for ensuring unique column identifiers are commonly used
outside of MySQL - e.g. SQL Server, Postgres - where auto_inc is not
available?


I use triggers on Oracle or MS-SQL Server.

Regards,
Matthias
Jul 17 '05 #4
Tom Lee wrote:
Agreed that simultaneous requests will cause problems, but what
mechanisms for ensuring unique column identifiers are commonly used
outside of MySQL - e.g. SQL Server, Postgres - where auto_inc is not
available?


Create a table called T_AUTO which has

___________
Table Field NextId
Then when you need a new ID, create a r/w connection to the table - get
the value and update it in one go.

Problem with this is that in MySQL it will be very slow as there is no
such thing as a row lock.

Seriously I have used the filesystem before for unique ids - by
creating a directory full of number.avail (e.g. 1.avail etc) ... then
my process tries to rename one of them to 1.unavail - when it succeeds
it uses it and puts some info in the file as to what it now points to.
The cool thing about file renames is they are one of the few fast
indivisible operations in almost all OSs.
--
Spam:newsgroup(at)cr*********@verisign-sux-klj.com
EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>
Jul 17 '05 #5
Could you go into that a bit more, I am not sure what you mean, sounds
interesting.

I need more than one "auto-increment" due to the schema of the table; in the
Vignette world I'm used to a separate "next_id" table but that would not
apply well in this version of mySQL due to performance.

Phil

"127.0.0.1" <newsgroup(at)cr*********@verisign-sux-ijlkl.com> wrote in
message news:s4*******************@news-server.bigpond.net.au...
Tom Lee wrote:
Agreed that simultaneous requests will cause problems, but what
mechanisms for ensuring unique column identifiers are commonly used
outside of MySQL - e.g. SQL Server, Postgres - where auto_inc is not
available?


Create a table called T_AUTO which has

___________
Table Field NextId
Then when you need a new ID, create a r/w connection to the table - get
the value and update it in one go.

Problem with this is that in MySQL it will be very slow as there is no
such thing as a row lock.

Seriously I have used the filesystem before for unique ids - by
creating a directory full of number.avail (e.g. 1.avail etc) ... then
my process tries to rename one of them to 1.unavail - when it succeeds
it uses it and puts some info in the file as to what it now points to.
The cool thing about file renames is they are one of the few fast
indivisible operations in almost all OSs.
--
Spam:newsgroup(at)cr*********@verisign-sux-klj.com
EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Ladasky | last post by:
Hi there, Just wanted to share a frustrating programming bug that, when I figured it out, made me gasp, and then laugh. In one of my programs I wrote... c = max(a,b) ....and I was...
23
by: Antoon Pardon | last post by:
I have had a look at the signal module and the example and came to the conclusion that the example wont work if you try to do this in a thread. So is there a chance similar code will work in a...
27
by: garyolsen | last post by:
In C++ what kind of unexpected conditions should be handled as exceptions? Besides dividing by 0, bad memory allocation, what're the most popular exceptions? When should not use exception,...
6
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
8
by: MLH | last post by:
A97 HELP shows the proper syntax for using Nz as Nz(variant) I'm wondering what to expect from potential past misuse I've made. For example, consider the following... Private Sub...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
12
by: jeniffer | last post by:
Can a static function defined in a C file be ever referred (called) externally from another C file?If so in which conditions?
3
by: lars.uffmann | last post by:
Hi everyone! I am debugging a big piece of code on the search for memory leaks, using g++ under suse 9.3. Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a class foo that is...
56
by: xlar54 | last post by:
I've been going through the newsgroup, picking up best practices and things not to do, as I think it helps to make a good programmer a very good one. But rather than fishing, I figure that there...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.