View Full Version : PHP acting upon match

09-10-2011, 08:22 AM
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.

09-10-2011, 08:32 AM
DELETE FROM table WHERE field = 0

09-10-2011, 08:39 AM
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.

if (MySQL table newsletters(email == $email + womens == 0) {

09-10-2011, 09:18 AM
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.

09-10-2011, 09:27 AM
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?

09-10-2011, 10:49 AM
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:

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?

09-10-2011, 09:37 PM
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.