我的PHP MySQL语法太长了

时间:2012-06-27 22:37:08

标签: php mysql

谢谢,非常好的解决方案,这已经回答了我的问题。 如果我的$ sql如下,我该如何使用你的解决方案:

$sql = "SELECT * FROM $tbl_main, $tbl_country, $tbl_members
WHERE ($tbl_main.country_id = $tbl_country.country_id) AND ($tbl_main.member_id = $theselect) ORDER BY $chosenTable.$orderby LIMIT $startpoint, $limit";

即。如何在代码中间的某处使用您的解决方案。

1 个答案:

答案 0 :(得分:4)

您可以有条件地连接额外条件:

$sql = "SELECT * FROM $tbl_main, $tbl_country, $tbl_members
WHERE ($tbl_main.country_id = $tbl_country.country_id)";

if(isset($theselect)) {
    $sql .= " AND ($tbl_main.member_id = $theselect)"; 
}

$sql .= " ORDER BY $chosenTable.$orderby LIMIT $startpoint, $limit";