I have a page where I want to use a very similar SQL query a number of times. The only difference will be that the WHERE clause will be slightly different, i.e. sometimes it will be "WHERE term_taxonomy_id = 4", sometimes "WHERE term_taxonomy_id = 7" and so on.
At the moment I have written out the whole query each time, but do I need to do this? Is there a way that I can use the same piece of code for all the queries, and just add the extra WHERE clause?
Here's the original code:
$sql = "SELECT post_id,
DATE_FORMAT(start,'%m/%d/%Y') AS eventStart,
DATE_FORMAT(end,'%m/%d/%Y') AS eventEnd,
DATE_FORMAT(CURDATE(),'%m/%d/%Y') AS today,
DATE_FORMAT(DATE_ADD(CURDATE(), INTERVAL 7 DAY),'%m/%d/%Y') AS endWeek
FROM wp_ec3_schedule s
JOIN wp_posts p ON p.ID = s.post_id
WHERE post_status = 'publish' AND term_taxonomy_id = 6 ";
$clause = array();
for( $i = 0; $i < 7; $i++ ) {
$clause[] = "DATE_ADD(CURDATE(), INTERVAL $i DAY) BETWEEN DATE(start) AND DATE(end)";
}
$sql .= "AND (" . implode(' OR ', $clause) . ")";
$sql .= ' ORDER BY start DESC';
$result = mysql_query($sql) or trigger_error($sql . ' has failed. <br />' . mysql_error()); //pull data from database.