Is there any special way for variable++?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • typingcat@gmail.com

    Is there any special way for variable++?

    Say I have a table in a MySQL server
    ID USERNAME COUNT
    0 JOHN 2
    1 JANE 3
    2 HOMER 3
    3 MOE 2

    If I want to increase Homer's count by 1,
    The plain way would be;
    1)$TEMP=SELECT COUNT WHERE ID=2
    2)$TEMP++
    2)UPDATE .. SET COUNT=$temp...
    I guess if there is any special way to increase the int value by one?
    For increasing 1 is so commonly used (that's why c,c++,java,C# has the
    operator ++).

  • Ewoud Dronkert

    #2
    Re: Is there any special way for variable++?

    typingcat@gmail .com wrote:
    [color=blue]
    > Say I have a table in a MySQL server
    > ID USERNAME COUNT
    > 0 JOHN 2
    > 1 JANE 3
    > 2 HOMER 3
    > 3 MOE 2[/color]

    Generally, it's a bad idea to give a column a name that is also a sql
    preserved word (count).
    [color=blue]
    > If I want to increase Homer's count by 1,
    > The plain way would be;
    > 1)$TEMP=SELECT COUNT WHERE ID=2
    > 2)$TEMP++
    > 2)UPDATE .. SET COUNT=$temp...[/color]

    No, you would let the database do the work:

    UPDATE Tablename SET count=count+1 WHERE id=2

    May need `` around count, because of problem mentioned above. See
    http://php.net/mysql-query and

    [color=blue]
    > I guess if there is any special way to increase the int value by one?
    > For increasing 1 is so commonly used (that's why c,c++,java,C# has the
    > operator ++).[/color]

    Yes, that operator also exists in PHP:


    --
    E. Dronkert

    Comment

    • jkimball4@gmail.com

      #3
      Re: Is there any special way for variable++?

      you can do this in the mysql itself depending on what server version
      you are using. try update .. set count=count+1 where id=2 and
      username="homer ";

      typingcat@gmail .com wrote:[color=blue]
      > Say I have a table in a MySQL server
      > ID USERNAME COUNT
      > 0 JOHN 2
      > 1 JANE 3
      > 2 HOMER 3
      > 3 MOE 2
      >
      > If I want to increase Homer's count by 1,
      > The plain way would be;
      > 1)$TEMP=SELECT COUNT WHERE ID=2
      > 2)$TEMP++
      > 2)UPDATE .. SET COUNT=$temp...
      > I guess if there is any special way to increase the int value by one?
      > For increasing 1 is so commonly used (that's why c,c++,java,C# has the
      > operator ++).[/color]

      Comment

      Working...