views:

44

answers:

2

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??

A: 

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.

Peter Smit
+1  A: 

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
AvatarKava
Actually my question was how do I include date check function in that mysql_query?? and one more how do I prevent sql injection??
mathew
Use mysql_real_escape_string() or prepared statements to prevent sql injection.
Naktibalda
@mathew For one, you should use as little as possible functions in query. If I'm right, then mysql `NOW()` function will prevent query from caching, which will reduce performance.Secondly for preventing SQL injection you could use in mysql - mysql_real_escape_string and do your query like that:`SELECT * FROM data1, data2, data3, data4 WHERE person='" .mysql_real_escape_string($name). "'`Also for performance consumption it's better to select columns you are going to need and not mark as **'*'**.
Eugene
@Eugene - You are correct about the query cache, but assuming a reasonably sized table and proper indexing, performance should be fine and we're talking about a micro-optimization here. The vast majority of sites will benefit much more from page-level caching.
AvatarKava