How to delete contents from a mysql database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: How to delete contents from a mysql database

Post by lovelf »

It works awesomely, thanks very much.

Off topic, I'm going to create a post about it, but I have the following function:

Code: Select all


 function rrmdir($dir) { 
   if (is_dir($dir)) { 
     $objects = scandir($dir); 
     foreach ($objects as $object) { 
       if ($object != "." && $object != "..") { 
         if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); 
       } 
     } 
     reset($objects); 
     rmdir($dir); 
   } 
 } 

To delete directories, I have a field inside this database that is path, where I get the path of the directory with its contents, I would like as I'm deleting the database entries to also delete the directories with the given function, how to?
Thanks in advance.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: How to delete contents from a mysql database

Post by twinedev »

Celauran wrote:True, but this also ensures that you're keeping the last five rows. id - 4 doesn't necessarily; id = MAX(id) - 3, say, could have already been removed for some other reason.
Very good point I didn't think about. (used to my systems, where rows are generally never deleted, just marked archived or deleted by a limiter (usually anything before a certain timestamp like logs) ;-)
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: How to delete contents from a mysql database

Post by lovelf »

I got it. Thanks
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: How to delete contents from a mysql database

Post by twinedev »

Then you either use the method of getting the information of last 5 entries, put those directories in an array, and then delete all files from the root directory you need to work with UNLESS they are in the array of recent 5. (Basically, use the code from inside the function on its own, modify it to check against that array using in_array() and then also remove the line to remove the directory (which would be the root)

If you have other things in the root, well then you will need to read all the entries...

Code: Select all

$intLimiter = FALSE;
$rsRecords = mysql_query('SELECT `id`,`path` FROM `site` ORDER BY `id` DESC LIMIT 5'); 
if ($rsRecords && mysql_num_rows($rsRecords)>0) {
    while ($aryTemp = mysql_fetch_assoc($rsRecords)) {
        if ($intLimiter===FALSE) {
            // This is the "5th oldest, so store it for the delete statement
            $intLimiter = $aryTemp['id'];
        }
        else {
            // We already processed row #5, so this is older delete it...
            rrmdir($aryTemp['path']);
        }
    }
    mysql_free_result($rsRecords);
}

mysql_query('DELETE FROM `site` WHERE `id` < '.$intLimiter);
Post Reply