Jump to content

php file unlink and delete old records Mysql


cobusbo

Recommended Posts

Hi, Ive got a few records stored in my database and would wish to remove records older than 30 days. The thing is I got a field in my database table called pprofilepic which contains the directory of an image stored on my host. I would like to delete the image before removing the record from the database. Would I need to use the while loop to do so?

 

Here is an example of database records output in php array

$Users2 = array(
  array('ID' => '1','Username' => 'Cobus','mxitid' => 'Debater','mxitlogin' => '','nick' => '','phone' => '','location' => '','rank' => '1','registered' => '1443554980','last_online' => '1446482291','ip1' => '165.255.107.53','ip2' => '165.255.107.53','pprofilepic' => 'uploads/resized_1_1444068533.jpg','aprove' => 'yes','pname' => '','psname' => '','pgender' => '','pmarital' => '','plocation' => '','phobbies' => '','pabout_myself' => '','pdob' => '0000-00-00'),
  array('ID' => '2','Username' => 'AKA IV LEAGUE','mxitid' => 'm61831903002','mxitlogin' => '1998xtreme25','nick' => 'king"othello','phone' => 'ZTE/V795','location' => 'ZA,,,,,,212881,29608915838,1387f57','rank' => '2','registered' => '1443555418','last_online' => '1446731775','ip1' => '1c6ff112add9dc7a5594dec967be0f7a','ip2' => '196.35.255.31','pprofilepic' => 'images/resized_2_1446642825.jpg','aprove' => 'no','pname' => 'Akaworldwide','psname' => '','pgender' => 'Male','pmarital' => 'Not Saying','plocation' => 'South Africa »»KZN»»Durban','phobbies' => 'Programming - watching & playing soccer ','pabout_myself' => 'Humble guy down to earth- also short tempered - so watch what to say to me:)','pdob' => '1998-03-25'),

And here is my Code I put together so far but not sure if im on the right track.

<?

$result9 = mysql_query("SELECT * FROM Users2 WHERE last_online < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY))");
        while($row = mysql_fetch_array($result9)){
         $ppic = $row['pprofilepic'];
$id = $row['ID'];

$unlink($ppic);
    $query = "DELETE FROM Users2 WHERE ID ='".$id."'"; 
    mysql_query($query);


}





?>

Link to comment
Share on other sites

For deleting the file using unlink, absolute path of the file is required where as pprofilepic contains the relative path.

Yes but I wish to know if my code will work. Should it be in the while loop? to unlink each record that is older than 30 days and remove those specific records from the database?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.