Glitch in php or count() in mysql?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Schraalhans Keukenmeester

    #1

    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
  • Ewoud Dronkert

    #2
    Re: Glitch in php or count() in mysql?

    Schraalhans Keukenmeester wrote:[color=blue]
    > $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[/color]

    YES! (Btw, does the ; in the query not pose trouble?)
    [color=blue]
    > but still there is something funny going on.[/color]

    Concurrent updates perhaps.

    --
    E. Dronkert

    Comment

    • J.O. Aho

      #3
      Re: Glitch in php or count() in mysql?

      Schraalhans Keukenmeester wrote:[color=blue]
      > 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.[/color]


      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.

      [color=blue]
      > I am wondering, is php messing up, or is this a mysql glitch, or am I
      > missing something here ?[/color]

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


      //Aho

      Comment

      • Jerry Stuckle

        #4
        Re: Glitch in php or count() in mysql?

        Schraalhans Keukenmeester wrote:[color=blue]
        > 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[/color]

        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.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Gordon Burditt

          #5
          Re: Glitch in php or count() in mysql?

          >I have a PHP script that adds messages to a simple MySQL Database.[color=blue]
          >(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[/color]

          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.
          [color=blue]
          >$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 ?[/color]

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

          Gordon L. Burditt

          Comment

          • Tony Marston

            #6
            Re: Glitch in php or count() in mysql?


            "J.O. Aho" <user@example.n et> wrote in message
            news:3rusigFla8 mtU1@individual .net...[color=blue]
            > Schraalhans Keukenmeester wrote:[color=green]
            >> 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.[/color]
            >
            >
            > 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.[/color]

            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
            This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL



            [color=blue]
            > 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.
            >
            >[color=green]
            >> I am wondering, is php messing up, or is this a mysql glitch, or am I
            >> missing something here ?[/color]
            >
            > it's the php code you write that most likely is the cause of the trouble.
            >
            >
            > //Aho[/color]


            Comment

            • J.O. Aho

              #7
              Re: Glitch in php or count() in mysql?

              Tony Marston wrote:
              [color=blue]
              > 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.[/color]

              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

              Comment

              • Tony Marston

                #8
                Re: Glitch in php or count() in mysql?


                "J.O. Aho" <user@example.n et> wrote in message
                news:3s1l8jFln4 j6U1@individual .net...[color=blue]
                > Tony Marston wrote:
                >[color=green]
                >> 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.[/color][/color]

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

                --
                Tony Marston

                This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL



                [color=blue]
                > 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[/color]


                Comment

                Working...