Jump to content
Larry Ullman's Book Forums

Ch 3 'Db_Sessions.Inc.Php' Query Quonfusion


Recommended Posts

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

  1. the field or date to be manipulated i.e. last_accessed
  2. 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.

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...