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 7 of 7
  1. #1
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts

    PHP acting upon match

    How can I make PHP lookup something and to act upon a match?
    I want to make it so If PHP finds a field in a MySQL table that = 0, then PHP will execute a command.
    I'm guessing it would go something like this?

    if (MySQL TABLE.FIELD = 0) {do delete command;}

    I already know how to delete in MySQL, its PHP doing a lookup and matching a MySQL field that I don't understand.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Code:
    DELETE FROM table WHERE field = 0

  • #3
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Sure, thats the delete command:

    I have a table called "newsletters":
    +----------------+------+--------+
    | email | mens | womens |
    +----------------+------+--------+
    | test1@test.com | 0 | 1 |
    | test2@test.com | 1 | 1 |
    +----------------+------+--------+

    What I want to do is make it so after PHP unregisters a user, it checks the database to see if they are signed up to any other newsletters, and if they arn't, then to delete the key (email) from the table:

    Unregistering an e-mail address from the mens newsletter would go something along the lines of this:

    $query = "INSERT INTO newsletters(email, mens) VALUES('$email', 0) ON DUPLICATE KEY UPDATE mens = 0";
    if (MySQL table newsletters(email == $email + womens == 0) {
    $query = "delete from testdb.newsletters where email='$email'";
    }


    This is the line where I need PHP to check, the rest of the code is fine. This line is made up - to give an idea of what I'm trying to do.
    Code:
    if (MySQL table newsletters(email == $email + womens == 0) {
    Last edited by Democrazy; 09-10-2011 at 09:18 AM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Code:
    DELETE FROM newsletters WHERE mens = 0 AND womens = 0
    You do not need PHP to do any of this for you. SQL is capable of deleting any record where neither newsletter is selected. You can specify any number of where clauses.
    Alternatively, the structure of the table can be modified so that the newsletter type and the email are the only two keys, both of which create a composite key. Having a record means they subscribe to at least one, no records mean they subscribe to neither. Seems a little overboard to do that IMO.

  • #5
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Hey, sure man it does seem a little bit overboard, but its what I understand so I am going to stick with it. Kind of like in another thread where you advised me to use some "count" instruction. I know its a far more superior way as its smaller in commands, but I don't understand it.
    Time is my problem. I don't have much time right now, so I am sticking with what I know as much as possible. Its also good for my mental state of mind - I stress if there is no progress, so I would rather move forward and take a long way that I know, rather standing still trying to figure out the shorter way.

    How can I make it so MySQL will delete entries if values are all 0?

    Its probably better I just use the PHP line I'm looking for?
    Last edited by Democrazy; 09-10-2011 at 09:48 AM.

  • #6
    Banned
    Join Date
    Sep 2011
    Posts
    140
    Thanks
    17
    Thanked 0 Times in 0 Posts
    I've really trashed up this thread. I will start over:

    I want to make it so PHP will lookup fields in a table and if they match, it will then execute a command:

    +----------------+------+--------+
    | email | cars | planes |
    +----------------+------+--------+
    | test1@mail.com | 0 | 1 |
    | test2@mail.com | 1 | 1 |
    +----------------+------+--------+
    I'm guessing it would be something like this:
    Code:
    if (MySQL table newsletters(email = $email + cars = 0) {COMMAND;}
    I'm partially right, but don't know what to use inside the brackets. How should it be done?

  • #7
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    I'm really confused about what exactly you're looking for. The best solution is with an SQL query. The WHERE clause is a conditional. The DELETE query will remove all items in a table determined by the WHERE statements. You can use AND, OR statements in the where statement too.

    The MySQL will go through every row, check the fields specified in the WHERE clause against the condition, and act appropriately. If you choose to search for this value in PHP, you will have to run the DELETE query in exactly the same way anyway, so it makes no sense to gather information in the PHP before - unless you want some sort of record of deleted rows saved.

    If you're looking to do a SELECT based on certain values, you would still use the WHERE clause and specify the conditions you want from your results. That way, the MySQL will only return rows that match your criteria, no matter what they will be.


  •  

    Posting Permissions

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