PHP function in SQL query

Hi there,

I know that this is probably a really simple problem to fix but I can’t really find any info on it.

I’ve got a function called ‘Duration()’ which calculates how many hours have passed on a date.

However I would like to use the function within a SQL query.

$result = mysql_query("SELECT * FROM eagle6_edb.edb_scans WHERE solarSystemID = '$solarSystemID' AND Duration(date) < 48 ORDER BY id DESC");
$probing_num = mysql_num_rows($result);

Doesn’t seem to be working?

I know the function works fine as I’m using it else where…


cant remember the function but depending on how date is stored i think mysql has a function that does that anyway.

You can’t use a PHP function in a query unless your returning a value and embedding it into the query. You would either need to calculate it before hand and embed or use a MySQL equivalent function or series of functions.

I wouldn’t rewrite the Duration function to MySQL function(s), but instead rewrite the query to

  solarSystemID = '$solarSystemID'
  date > NOW() - INTERVAL '48' HOUR
  id DESC

Should be more efficient imo :slight_smile:

(query assumes the date column is of type datetime)