kwandoa Posted April 29, 2013 Share Posted April 29, 2013 Hi Would you be so kind as to break down and explain the following: // Delete old sessions: $q = sprintf('DELETE FROM sessions WHERE DATE_ADD(last_accessed, INTERVAL %d SECOND) < NOW()', (int) $expire); Link to comment Share on other sites More sharing options...
margaux Posted April 29, 2013 Share Posted April 29, 2013 There are 2 functions mainly at work here: sprintf() is a php function and DATE_ADD is a mysql function. sprintf is a function which takes some input, formats it according to the specfied arguments and returns the formatted input . The first argument is the input to be formatted and includes a formatting instruction - DELETE FROM session WHERE DATE_ADD(last_accessed, INTERVAL %d SECOND) < NOW(). You can think of %d in this context as fulfilling 2 roles - it is a placeholder and a type specifier. The percentage sign indicates that a variable will go in this position and be formatted according to d's meaning. There are many different type specifiers, d means translate the variable into an integer. Other optionsinclude binary integer, ASCII integer or string. The next argument is the specific variable to be formatted - (int)$expire. It is using (int) to force the variable to be an integer. If $expire equals 20, the output from this sprintf statement would be - DELETE FROM sessions WHERE DATE_ADD(last_accessed, INTERVAL 20 SECOND) < NOW() DATE_ADD lets you do some math on dates. It takes several arguments the field or date to be manipulated i.e. last_accessed the constant INTERVAL followed by the unit that you wish to add to the 1st argument - $expire seconds The whole statement says DELETE those rows from the sessions table if last_accessed plus $expire seconds is less than NOW(). Play around with different values and echo out the variable q to see how sprintf works. Hope this helps. 1 Link to comment Share on other sites More sharing options...
kwandoa Posted May 1, 2013 Author Share Posted May 1, 2013 great!! Link to comment Share on other sites More sharing options...
Recommended Posts