News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

mysql row delete php

Started by glennmckenna, August 16, 2016, 04:27:46 PM

Previous topic - Next topic

glennmckenna

i'm trying to create a script that will load the database and if the date is passed delete that row else do nothing

here is the current status of the code:

<?php
/**
* Created by PhpStorm.
* User: glenn
* Date: 16/08/2016
* Time: 16:40
*/
require_once('../scripts/SSI-link.php');
$affiche = $smcFunc['db_query']('', 'SELECT `ID`, `expdate` FROM mdlpalissvbeta.homeaffichebanner', '');
$num_rows = $smcFunc['db_num_rows'] ($affiche);
$today = date("Y-m-d");

while (
$homeaff = $smcFunc['db_fetch_assoc']($affiche)){
   if (
expdate > $today){
       echo
'old';
       echo
'<pre>', print_r($homeaff), '</pre>';
   }else {
       echo
'new';
       echo
'<pre>', print_r($homeaff), '</pre>';
   }
}


?>


but i keep on getting error 500

glennmckenna

right after doing a bit of Reading it seems that I'm going about this the wrong way cause i could all of this in one mysql command

like on this site
http://stackoverflow.com/questions/13871414/mysql-delete-operation-comparing-datetime-with-current-date-time
http://stackoverflow.com/questions/10197633/mysql-query-to-delete-rows-whose-timestamp-is-older-than-current-timestamp

also what i would like to do is add it to the scheduled tasks of smf
http://www.simplemachines.org/community/index.php?topic=329894.0

so i assume that i would just have to do something like this (in Sources/ScheduledTasks.php):


function scheduled_delhomead()
{
global $modSettings, $sourcedir, $mbname, $txt, $smcFunc, $scripturl;

$date = DATE(y-m-d);
$query = 'DELETE FROM homeaffichebanner
WHERE homeaffichebanner.expdate <= {int:date}';

$result = $smcFunc['db_query']('',
$query,
array(
'date' => $date,
)
);


is this correct ?

leghorn23

Assuming your DB time matches your server time:


function scheduled_delhomead()
{
global $modSettings, $sourcedir, $mbname, $txt, $smcFunc, $scripturl;

$query = 'DELETE FROM homeaffichebanner WHERE homeaffichebanner.expdate <= ' . date("Y-m-d");

$result = $smcFunc['db_query']('',
$query,
);
}

Advertisement: