473,408 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Glitch in php or count() in mysql?

X-Followup: comp.lang.php

I have a PHP script that adds messages to a simple MySQL Database.
(PHP 5.0.3, MySQL 4.1.1)

One of the fields it stores is msgid.
The new msgid is a count of all current msgs in the db plus one

$query = 'select count(*) from messagesdb;';
$result = mysql_query ($query, $conn);
$msgid = mysql_result ($result, 'count(*)') + 1;

The next message is added using the above msgid.
For some reason (there are NO other scripts/systems accessing this
table, it is all on a local testmachine) I now have about 200 messages
in the system, but some id's occur more than once, up to 4 times.

I cannot explain this behaviour. I know I could avoid the entire issue
by autonumbering the messages, but still there is something funny going on.

I am wondering, is php messing up, or is this a mysql glitch, or am I
missing something here ?

Thanks for your time
Sh
Oct 22 '05 #1
7 2268
Schraalhans Keukenmeester wrote:
$query = 'select count(*) from messagesdb;';
$result = mysql_query ($query, $conn);
$msgid = mysql_result ($result, 'count(*)') + 1;

I know I could avoid the entire issue
by autonumbering the messages
YES! (Btw, does the ; in the query not pose trouble?)
but still there is something funny going on.


Concurrent updates perhaps.

--
E. Dronkert
Oct 22 '05 #2
Schraalhans Keukenmeester wrote:
X-Followup: comp.lang.php

I have a PHP script that adds messages to a simple MySQL Database.
(PHP 5.0.3, MySQL 4.1.1)

One of the fields it stores is msgid.
The new msgid is a count of all current msgs in the db plus one

$query = 'select count(*) from messagesdb;';
$result = mysql_query ($query, $conn);
$msgid = mysql_result ($result, 'count(*)') + 1;

The next message is added using the above msgid.
For some reason (there are NO other scripts/systems accessing this
table, it is all on a local testmachine) I now have about 200 messages
in the system, but some id's occur more than once, up to 4 times.

Why don't you make your msgid column in the mysql to an AUTO_INCREMENT?
There is a flaw in your code that can cause problems if there happens to be
more than one person who adds something to the database, the

$query = 'select count(*) from messagesdb;';

may be executed more than once before a new row is inserted, which leads to
multiple use of the same msgid.

When you insert your 201st row and then delete row 198, and then insert a new
row again, the id will be 201 again and you will end up with multiples of the
same msgid.

You should add the UNIQUE to the megid and maybe even PRIMARY KEY too, so that
you will not have this kind of troubles of multiple msgids.

I am wondering, is php messing up, or is this a mysql glitch, or am I
missing something here ?


it's the php code you write that most likely is the cause of the trouble.
//Aho
Oct 22 '05 #3
Schraalhans Keukenmeester wrote:
X-Followup: comp.lang.php

I have a PHP script that adds messages to a simple MySQL Database.
(PHP 5.0.3, MySQL 4.1.1)

One of the fields it stores is msgid.
The new msgid is a count of all current msgs in the db plus one

$query = 'select count(*) from messagesdb;';
$result = mysql_query ($query, $conn);
$msgid = mysql_result ($result, 'count(*)') + 1;

The next message is added using the above msgid.
For some reason (there are NO other scripts/systems accessing this
table, it is all on a local testmachine) I now have about 200 messages
in the system, but some id's occur more than once, up to 4 times.

I cannot explain this behaviour. I know I could avoid the entire issue
by autonumbering the messages, but still there is something funny going on.

I am wondering, is php messing up, or is this a mysql glitch, or am I
missing something here ?

Thanks for your time
Sh


In addition to what the others have said, if you ever delete a message
from the table, COUNT(*) will be decremented and you'll have duplicate
values.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 22 '05 #4
>I have a PHP script that adds messages to a simple MySQL Database.
(PHP 5.0.3, MySQL 4.1.1)

One of the fields it stores is msgid.
The new msgid is a count of all current msgs in the db plus one
If you ever delete a message, this will mess up. It would work
better if you make the new msgid the maximum of the message ids in
the db plus one. This only screws up if you delete the previously
highest msgid.

There is also a possibility of messing up if two copies of the script
running at the same time both do their select, then both do their
inserts.

It's exactly this problem that auto_increment solves.
$query = 'select count(*) from messagesdb;';
$result = mysql_query ($query, $conn);
$msgid = mysql_result ($result, 'count(*)') + 1;

The next message is added using the above msgid.
For some reason (there are NO other scripts/systems accessing this
table, it is all on a local testmachine) I now have about 200 messages
in the system, but some id's occur more than once, up to 4 times.

I cannot explain this behaviour. I know I could avoid the entire issue
by autonumbering the messages, but still there is something funny going on.

I am wondering, is php messing up, or is this a mysql glitch, or am I
missing something here ?


If the msgid is supposed to be unique, you should have a unique index
on it.

Gordon L. Burditt
Oct 22 '05 #5

"J.O. Aho" <us**@example.net> wrote in message
news:3r************@individual.net...
Schraalhans Keukenmeester wrote:
X-Followup: comp.lang.php

I have a PHP script that adds messages to a simple MySQL Database.
(PHP 5.0.3, MySQL 4.1.1)

One of the fields it stores is msgid.
The new msgid is a count of all current msgs in the db plus one

$query = 'select count(*) from messagesdb;';
$result = mysql_query ($query, $conn);
$msgid = mysql_result ($result, 'count(*)') + 1;

The next message is added using the above msgid.
For some reason (there are NO other scripts/systems accessing this
table, it is all on a local testmachine) I now have about 200 messages
in the system, but some id's occur more than once, up to 4 times.

Why don't you make your msgid column in the mysql to an AUTO_INCREMENT?
There is a flaw in your code that can cause problems if there happens to
be
more than one person who adds something to the database, the

$query = 'select count(*) from messagesdb;';

may be executed more than once before a new row is inserted, which leads
to
multiple use of the same msgid.

When you insert your 201st row and then delete row 198, and then insert a
new
row again, the id will be 201 again and you will end up with multiples of
the
same msgid.


You should be using 'select max(msg_id) from messagesdb;' instead of 'select
count(*) from messagesdb'. In this way it will not matter if any previous
entries get deleted.

--
Tony Marston
http://www.tonymarston.net
You should add the UNIQUE to the megid and maybe even PRIMARY KEY too, so
that
you will not have this kind of troubles of multiple msgids.

I am wondering, is php messing up, or is this a mysql glitch, or am I
missing something here ?


it's the php code you write that most likely is the cause of the trouble.
//Aho

Oct 23 '05 #6
Tony Marston wrote:
You should be using 'select max(msg_id) from messagesdb;' instead of 'select
count(*) from messagesdb'. In this way it will not matter if any previous
entries get deleted.


This can still generate doublets of msgid as the select statment can be
trigged by two different people trying to add something,
AUTO_INCREMENT UNIQUE should in most cases be used for ID (numerial) columns.
//Aho
Oct 23 '05 #7

"J.O. Aho" <us**@example.net> wrote in message
news:3s************@individual.net...
Tony Marston wrote:
You should be using 'select max(msg_id) from messagesdb;' instead of
'select
count(*) from messagesdb'. In this way it will not matter if any previous
entries get deleted.

Not if you lock the table bfeore performing the 'select max(id)' and the
'insert'.

--
Tony Marston

http://www.tonymarston.net
This can still generate doublets of msgid as the select statment can be
trigged by two different people trying to add something,
AUTO_INCREMENT UNIQUE should in most cases be used for ID (numerial)
columns.
//Aho

Oct 24 '05 #8

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

Similar topics

15
by: David O'Farrell | last post by:
Is this only on solaris ? Python 2.3.3 (#1, Mar 19 2004, 16:18:33) on sunos5 Type "help", "copyright", "credits" or "license" for more information. >>> a= >>> print a.count(333),...
0
by: motorpsychkill | last post by:
I have two tables: bc_post and bc_message. This is what I have so far: select bc_post.*, count(bc_message.message_to) responses from bc_post left join bc_message on bc_post.post_id =...
0
by: Fatt Shin | last post by:
Hi, I'm running MySQL 4.0.13, connecting from PowerBuilder 9 using ODCB Connector 3.51. I'm facing a problem where whenever I issue a SELECT COUNT(*) statement from PowerBuilder, I always get SQL...
2
by: TGOS | last post by:
Here's a little question to MySQL Gurus. Two tables. One called 'document' one called 'page'. Needles to say, a document consist out of multiple pages (1:n). The primary (P) and foreign (F) keys...
1
by: Eddie | last post by:
I am having a strange issue with MySQL. I just installed it on my Zaurus and loaded a few of my databases in it. All very small. When I do a count withoout a where, it hangs. It does this on all...
11
by: Ted Mayett | last post by:
OK. Here is a glitch, sorry if this has been mentioned before. This is an erratic glitch. I am now up to three other people besides myself who have been able to see this glitch. It seems it...
3
by: auron | last post by:
Hi there, I have a really stupid and banal problem with showing the results of a MySQL query in PHP, preciselly with MySQL count() function that gives to a variable in PHP the result. NOTE:...
0
by: Luongo | last post by:
Hi, I'm working on a mysql-based survey program. On the user page I display the survey form, and on the admin page I display the results for each question. The problem, which I find puzzling, is...
3
by: Auddog | last post by:
I have the following query that works in mysql: select id, order_no, price, count(item_no), sum(price) from production WHERE item_no = '27714' group by item_no; When I setup my query in php,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.