Increment the specific field by 1 in a query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #1

    Increment the specific field by 1 in a query

    I still do the process of incrementing a specific field in a stand alone apps by

    query 1: get its value
    query 2: update value +1


    But, i realize that this is really a bad idea when this would be used in web applications... specially the database is in another webhost....(ver y slow response)

    i tried (using php function)

    [CODE=mysql]UPDATE tablename SET field = field + 1;[/CODE]

    but it fails,

    Any suggestions or tutorials?
    Last edited by Atli; Nov 1 '08, 03:28 PM. Reason: Added [code] tags.
  • benmanns
    New Member
    • Oct 2008
    • 4

    #2
    [code=mysql]UPDATE `tablename` SET `field` = `field` + 1 WHERE `id` = 123;[/code]
    Replace 123 with the row id and it should work.
    Here is a php version:
    [code=php]<?php
    $id = 123;
    mysql_connect() ;
    mysql_query("UP DATE `tablename` SET `field` = `field` + 1 WHERE `id` = $id;")
    ?>[/code]

    Comment

    Working...