Tutorial :How do I integrate date check function in single mysql query?



Question:

this is my current function:

$result = $db->sql_query("SELECT * FROM data1,data2,data3,data4 WHERE person='" .$name. "'");    $row = $db->sql_fetchrow($result);  $day = $row['regtime'];  $days = (strtotime(date("Y-m-d")) - strtotime($row['regtime'])) / (60 * 60 * 24);  if($row > 0 && $days < 15){    $row = ['name'];  $row = ['age'];    //etc  

but above function does double read in regtime row. what I am looking is can I club both function into single query?? if yes how??


Solution:1

Are you saying you only want rows from the last 15 days in the result set?

You don't include how you're joining the data1-4 tables and your person field is open to SQL injection, so I'm leaving those parts out, but included below is the way to filter out only records from the last 15 days:

  SELECT *       FROM data1     WHERE person = :name       AND regtime >= NOW() - INTERVAL 15 DAY  


Solution:2

Currently you only have one query in this function. If there is nothing missing in your excerpt, there is no double query. If you mean that you use twice $row['regtime'], that is an array read and not a query.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »