Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder cyrus709's Avatar
    Join Date
    Dec 2008
    Posts
    102
    Thanks
    22
    Thanked 5 Times in 5 Posts

    subtract from mySQL data without negative number?

    I have a question

    How can i subtract from a mysql DB, using php without allowing the number in the DB to go below '0'?
    an example is
    PHP Code:
    <? 
    $C 
    mysql_connect("host","user","pass");
        if(!
    $C)
     {
         die();
    }
    mysql_select_db("database"$C);
    mysql_query("UPDATE table SET col = col - 1");
    ?>
    alright so the code works, but it can give me a negative number and i dont want to be able to have a negative number. im pretty sure that the code would be something like this?
    PHP Code:
    <? 
    $C 
    mysql_connect("host","user","pass");
        if(!
    $C)
     {
         die();
    }
    mysql_select_db("database"$C);
    $var mysql_query("UPDATE table SET col = col - 1");
    if (
    $var 0)
    {
    print 
    "Cannot perform action";
    }
    ?>
    but that would not stop it i think instead of print it should be die() but that does not work either
    Last edited by cyrus709; 03-15-2009 at 02:12 AM.
    Last night I lay in bed looking up at the stars in the sky and I thought to myself, where the heck is the ceiling.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Your subject line is horrible. Help us help you. From http://www.codingforums.com/postguide.htm: "2) Type in a subject that summarizes your question- A great way to turn off other members wanting to help you is to type an incomplete or silly subject for your post."

    Now, I think your update can be handled directly in SQL rather than PHP, but that assumes you don't want any feedback from the db on whether is was able to decrement and what the value is. If this is a real application, you probably want more information.

    The direct SQL fix might look like this, but I'm not quite sure if it will work:
    Code:
    UPDATE table SET col = col - 1 WHERE col > 0;
    You second code attempt won't work because you're mis-using the result from mysql_query. Read the man page to figure out what you're doing wrong.
    Are you a Help Vampire?

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Your subject line is horrible.
    Yes, one of the worst title I've ever seen here in this forum. Please edit your post and change that title.
    Thanks
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    Quote Originally Posted by abduraooft View Post
    Yes, one of the worst title I've ever seen here in this forum. Please edit your post and change that title.
    Thanks
    I agree, change it to something like How to subtract from mySQL data without getting lower than one or something like that....


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •