So I've created a program in PHP and MongoDB for my website in which I can see useful data about my members, and filter through them.
A query could for example look like: Show me everybody who's Male, between 20 and 30, has visited the site in the past 7 days, and logged in to the site at least 10 times.
With the query above, I would get for example 100 people, and I want to set an automated email (I have the email functions) specific to the filters.
So, in this case, 100 emails would be sent, but every time a member triggers the above query (when a user logs in and matches the above query, for example the person could change his sex from Male to Female and perhaps now meet the query), the event would be triggered and an email would be sent.
How would you go about 'storing' these kind of queries, and actually testing them against the members when they visit the site?
Ps. The queries can be unknown to me, as other people could be using my website, so I cannot run an "if" logic when users login, as I don't know the query criteria of other people.
A rules engine would be most appropriate for this task. Since you're using PHP, I would suggest taking a look at the Ruler project from Justin Hileman. This, and rules engines in general, allows you to define rules (conceptually similar to DB query criteria), which in evaluate a context (one or more values) and return a boolean result. Most engines will allow you to programmatically construct rules, which means that you can store their configurations in a database (ideal if users are creating their own criteria).
An excellent blog post on implementing Ruler can be found on Jon Wage's blog, wherein he describes how Ruler is in use at e-commerce shop OpenSky. They make heavy use of it for analytics and customer rewards (e.g. if a user refers a friend, who then purchases a product within X days, the original user receipts a credit).
Although not covered heavily the blog post, OpenSky does have a backend UI for creating and managing rules. Individual conditions such as "is user from Facebook" are implemented in PHP, but a web UI allows complex rules to be crafted, and that configuration is, in turn, stored in the database.