How to use cron, PHP and MySQL code to delete records with multiple search terms

cron, mysql, php, records

Solution

This should at least get you started. Keep in mind I don't know the structure of your DB so you may have to change some things. Also this is by FAR not the most efficient way to do it, but it is easy to follow, you can do this in a single MySQL query instead of looping over each user id (which I show an untested example of see ---faster way---), but again I don't know about your DB structure so you'll have to play with it, plus this way does exactly what you do now, one at a time so hopefully it's easier to follow and you can mod it to be more efficient later. Backup your db before playing with this.

PHP script to do what you want:

<?php
//put your search terms in this array
$search_terms = array('yahoo', 'google', 'bing');

//conenct to your DB
$db_conn = mysql_connect('yourdbhost', 'yourdbuser', 'yourdbpassword');
mysql_select_db('yourdbname', $db_conn);

//query the db for each search term
foreach($search_terms as $search_term) {
    //----slow but clear way----    
    $result = mysql_query("SELECT user_id 
                        FROM user
                        WHERE EXISTS (
                            SELECT user_id
                            FROM userinfo
                            WHERE essay like '%{$search_term}%' 
                            AND user.user_id = userinfo.user_id)", $db_conn);

    //for eachr user id returned, delete them from your users table 
    //as you would have done manually. I don't know about your users
    //table so you will have to edit this query as you see fit.
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $user_del_result = mysql_query("DELETE FROM user 
                                        WHERE user_id = {$row['user_id']}
                                        LIMIT 1", $db_conn);
        //might want to check here to see if the query executed successfully
    }

    //----faster way----
    $result = mysql_query("DELETE FROM user
                            WHERE EXISTS (
                            SELECT user_id
                            FROM userinfo
                            WHERE essay like '%{$search_term}%' 
                            AND user.user_id = userinfo.user_id)", $db_conn);
}
?>

To call it periodically you need to have a look at cron jobs (read this) and then just call the PHP script periodically with something like this in your crontab (this would run once per day at midnight for instance):

0 0 * * *   /path/to/php-cli /path/to/the/above/php/file.php

Problem

I’m in a deep s**t and need help from smart fellows like you. Here is some info about software and technologies I use: cPanel, Linux server, phpMyAdmin, MySQL. I need to delete some records from a table on a daily basis. I do it normally by using Select statement on phpMyAdmin and then delete all the records. * ``` Select `user_id` FROM `user` WHERE EXISTS ( SELECT `user_id` FROM `userinfo` WHERE `essay` like '% yahoo %' AND user.user_id = userinfo.user_id) ``` * The above code basically searches for specific words in userinfo table and displays their corresponding user_id in user table, which I then delete manually. In this particular case the search term is ‘yahoo’. I search for all the users who have the word ‘yahoo’ in their profile’s essay and delete them from user table. The problem is I’ve got way too many search terms to do it manually. I would love to have it written in php file and run through cron if it’s possible. Would be nice to have a field or fields on php file where I would type all possible search words/phrases then the code would execute all of them one after another. I’ve heard it’s possible to use cron jobs for that, read some articles about them but have absolutely no idea how to handle it. Your help would be much appreciated and please tell me if it’s unclear. Regards, Igor

Original source