SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: CASE

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    CASE

    Based on http://dev.mysql.com/doc/refman/5.0/...statement.html, I think I have to store my query before I run it. How do I store and then execute it?

    My code so far produces and error:
    PHP Code:
    $query "UPDATE plan SET curacc = case when curacc + 1 >= ddecpt 
                           then 0
                           else curacc + 1
                      end,
           curwipprice =    case when curacc + 1 >= ddecpt
                           then curwipprice + 1
                           else curwipprice
                      end
       WHERE id = 'LAMS-ZV90'"
    ;
       
       include 
    "connect_to_mysql.php";
     echo 
    $query '<br/>'
    mysql_query(" . $query . ") or die(mysql_error()); 

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,268
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    the query looks okay

    what was the error?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Here's the error I received: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '. UPDATE plan SET curacc = case when curacc + 1 >= ddecpt ' at line 1

    I would've posted it earlier, but I didn't think I was close enough for it to matter.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,268
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    your query would be fine if you can track down where that period in front of UPDATE came from, and remove it

    i don't do php but my guess is, it came from here --
    PHP Code:
    mysql_query(" . $query . ") or die(mysql_error()); 
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Posts
    453
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Good guess r937! That did the trick.

    That was a nice surprise after mowing the lawn.


Bookmarks

Posting Permissions

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