function get_tags_by_criteria($gender_id, $country_id, $region_id, $city_id, $day_of_birth=NULL, $tag_id=NULL, $thread_id=NULL) {
$query = "SELECT tags.*
FROM tags, thread_tag_map, threads
WHERE thread_tag_map.thread_id = threads.id
AND thread_tag_map.tag_id = tags.id
AND threads.gender_id = $gender_id
AND threads.country_id = $country_id
AND threads.region_id = $region_id
AND threads.city_id = $city_id
AND tags.id LIKE '%$tag_id%'
AND threads.id LIKE '%$thread_id%'";
if(!$day_of_birth)
{
$query += "AND threads.min_day_of_birth <= '$day_of_birth AND threads.max_day_of_birth >= '$day_of_birth' ";
}
$query += "GROUP BY tags.name";
$result = $this->do_query($query);
return $result;
}
如果沒有$ day_of_birth作爲參數傳遞,我希望sql忽略if中的2行。我用:如果函數裏面的sql查詢?
$all_tags = $forum_model->get_tags_by_criteria(1, 1, 0, 0);
我不知道爲什麼這個SQL返回一個錯誤:
Couldn't execute query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0' at line 1
最後的查詢看起來像什麼? echo $ query; – Galen 2009-12-09 16:53:46
+1給蓋倫,只是迴應這個問題會讓你意識到很多人下面指出的問題。 – Simon 2009-12-09 16:57:12