#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Unhappy php and mysql if else statement?


    I have this problem, and I have only a little knowledge in PHP and mysql.

    I want to know can I use condition statement like this in PHP?

    SELECT database FROM xxx WHERE xxx= 'yes' FOR id= 1

    then
    UPDATE yyy + 1
    meaning that I select xxx column which has the value of yes, if it's a yes then column yyy + 1 for id=1.

    Can anyone help me in writing a proper line in PHP where the code can change the database value.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,632
    Rep Power
    1811
    You can't, really, do both at once; you can either SELECT or UPDATE. The good news is that both allow the use of the WHERE clause so:
    UPDATE your_table SET column=column+1 WHERE another_column='yes'
    I am not sure what you are doing with the 'FOR id = 1' part, that looks like you also want to restrict the update further to just records where the id column is 1. In which case just add 'AND id = 1' to the end of the WHERE clause.
    If you are not sure what impact that will have just convert the query to a SELECT and check the resulting rows to ensure they are the ones you'd expect.

    There does not seem to be much php here so it may have been better off asked in the mySQL forum.

    Comments on this post

    • s-p-n agrees : UPDATE database SET yyy=yyy+1 WHERE xxx='yes' AND id=1
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,200
    Rep Power
    495
    Originally Posted by zeebramanx
    I have this problem, and I have only a little knowledge in PHP and mysql.

    I want to know can I use condition statement like this in PHP?

    SELECT database FROM xxx WHERE xxx= 'yes' FOR id= 1

    then
    UPDATE yyy + 1
    meaning that I select xxx column which has the value of yes, if it's a yes then column yyy + 1 for id=1.

    Can anyone help me in writing a proper line in PHP where the code can change the database value.
    First, you grab the data from the existing record by using the "SELECT" statement. Drop the result of that into a variable and then check IF the variable equals something, THEN do something, otherwise (ELSE) do something else.

    Google is littered with thousands of examples and how-tos on reading data from a database and another billion or so sites explain the IF / ELSE conditions in great detail and provide examples.
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0

    Talking


    Originally Posted by SimonJM
    You can't, really, do both at once; you can either SELECT or UPDATE. The good news is that both allow the use of the WHERE clause so:
    UPDATE your_table SET column=column+1 WHERE another_column='yes'
    I am not sure what you are doing with the 'FOR id = 1' part, that looks like you also want to restrict the update further to just records where the id column is 1. In which case just add 'AND id = 1' to the end of the WHERE clause.
    If you are not sure what impact that will have just convert the query to a SELECT and check the resulting rows to ensure they are the ones you'd expect.

    There does not seem to be much php here so it may have been better off asked in the mySQL forum.
    Thank you my friend! it helps though!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2014
    Posts
    6
    Rep Power
    0
    Originally Posted by badger_fruit
    First, you grab the data from the existing record by using the "SELECT" statement. Drop the result of that into a variable and then check IF the variable equals something, THEN do something, otherwise (ELSE) do something else.

    Google is littered with thousands of examples and how-tos on reading data from a database and another billion or so sites explain the IF / ELSE conditions in great detail and provide examples.
    Thank you very much my friend! I will try it out.
  10. #6
  11. No Profile Picture

    Join Date
    Jun 2014
    Posts
    41
    Rep Power
    4

    Smile


    Originally Posted by zeebramanx
    I have this problem, and I have only a little knowledge in PHP and mysql.

    I want to know can I use condition statement like this in PHP?

    SELECT database FROM xxx WHERE xxx= 'yes' FOR id= 1

    then
    UPDATE yyy + 1
    meaning that I select xxx column which has the value of yes, if it's a yes then column yyy + 1 for id=1.

    Can anyone help me in writing a proper line in PHP where the code can change the database value.

    UPDATE table_name
    SET column1=value, column2=value2,...
    WHERE some_column=some_value

    you should to try form this site :
    w3schools
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,350
    Rep Power
    630
    Bad advice. W3schools is obsolete, inaccurate and in many cases just plain wrong. DO NOT use W3schools for anything.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,553
    Rep Power
    4549
    Originally Posted by gw1500se
    Bad advice. W3schools is obsolete, inaccurate and in many cases just plain wrong. DO NOT use W3schools for anything.
    No, this is bad advice and has no place in these forums.
    ======
    Doug G
    ======
    I've never been able to appreciate the sublime arrogance of folks who feel they were put on earth just to save other folks from themselves .." - Donald Hamilton
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,350
    Rep Power
    630
    Correcting bad advice always has a place in any help forum. I refer you to http://www.w3fools.com.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo