cobusbo Posted November 5, 2015 Share Posted November 5, 2015 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); } ?> Quote Link to comment Share on other sites More sharing options...
crazycoder Posted November 5, 2015 Share Posted November 5, 2015 For deleting the file using unlink, absolute path of the file is required where as pprofilepic contains the relative path. Quote Link to comment Share on other sites More sharing options...
cobusbo Posted November 6, 2015 Author Share Posted November 6, 2015 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 6, 2015 Share Posted November 6, 2015 Yes. But unlink() is a function, not a variable. Quote Link to comment Share on other sites More sharing options...
crazycoder Posted November 7, 2015 Share Posted November 7, 2015 Yes. You will require the loop to delete the images. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.