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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts

    Adding one to a variable

    All,
    I have the following code and it should work but it isn't:
    PHP Code:
            $qrynum "Select MAX(picture_id) as picture_id from pictures";
            
    $resultnum mysql_query($qrynum);
            
    $resultsetnum mysql_fetch_array($resultnum);
            echo 
    $resultsetnum['picture_id'];
            
    $newnum $resultsetnum['picture_id']+1
    Any idea what I'm doing wrong?

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    You should use the WWW rule here
    What did you try, What did you get, What did you expect
    You need to provide more info. What is the echo of $resultsetnum['picture_id'] returning?
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    I tried this:
    PHP Code:
            $qrynum "Select MAX(picture_id) as picture_id from pictures";
            
    $resultnum mysql_query($qrynum);
            
    $resultsetnum mysql_fetch_array($resultnum);
            echo 
    $resultsetnum['picture_id'];
            
    $newnum $resultsetnum['picture_id']+1;
            
    $filename"gallery-img$newnum.jpg";
            echo 
    $filename
    I also tried:
    PHP Code:
            $qrynum "Select MAX(picture_id) as picture_id from pictures";
            
    $resultnum mysql_query($qrynum);
            
    $resultsetnum mysql_fetch_array($resultnum);
            echo 
    $resultsetnum['picture_id'];
            
    $newnum $resultsetnum['picture_id']++;
            
    $filename"gallery-img$newnum.jpg";
            echo 
    $filename
    Both of these outputs produced:
    16
    gallery-img16.jpg

    It correctly pulls the max number, but it doesn't update it.

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Oh so you want to update the value in the database? If that's the case, you need to run a query on the DB to do this
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    No, I do update the update the database but I actually use the filename to upload a file and then update the db with that value. So I need to get the max picture_id (the picture_id and the filename are the same) from the datbase and then increment it by one so I don't have the same filename.

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    hmm I see. Can you do the following both before and after the update is meant to happen, and show the output
    PHP Code:
    var_dump($resultsetnum['picture_id']); 
    Also if you were trying
    PHP Code:
    $newnum $resultsetnum['picture_id']++; 
    you should have been using
    PHP Code:
    $newnum = ++$resultsetnum['picture_id']; 
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by JAY6390 View Post
    Also if you were trying
    PHP Code:
    $newnum $resultsetnum['picture_id']++; 
    you should have been using
    PHP Code:
    $newnum = ++$resultsetnum['picture_id']; 
    there is no difference in this context,

    best regards

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Yes there is watch...
    Run this
    PHP Code:
    $a 5;
    $b $a++;
    echo 
    $b.'<br />';

    $a 5;
    $b = ++$a;
    echo 
    $b.'<br />'
    The same code twice with only the ++ difference and yet the output is 5 for the first example, and 6 for the second. The reason is that $a only gets incremented AFTER the line has executed and assigned 5 to b in the first. It does it before the line executes in the second so it sets b to 6...
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by JAY6390 View Post
    Yes there is watch...
    Run this
    PHP Code:
    $a 5;
    $b $a++;
    echo 
    $b.'<br />';

    $a 5;
    $b = ++$a;
    echo 
    $b.'<br />'
    The same code twice with only the ++ difference and yet the output is 5 for the first example, and 6 for the second. The reason is that $a only gets incremented AFTER the line has executed and assigned 5 to b in the first. It does it before the line executes in the second so it sets b to 6...
    yes, you are right,

    best regards


  •  

    Posting Permissions

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