猿问

有没有办法让我不必在每个条件下编写完整查询?

有没有办法让我不必在每个条件下编写完整查询?


if($_GET['l'] == 1){

    $num = $db -> query("SELECT * FROM text WHERE type = '' AND gg = '1'") -> num_rows;

elseif($_GET['l'] == 2){

    $num = $db -> query("SELECT * FROM text WHERE type = '' AND gg = '2'") -> num_rows;

else {

    $num = $db -> query("SELECT * FROM text WHERE type = ''") -> num_rows;

}


慕田峪9158850
浏览 72回答 1
1回答

凤凰求蛊

很简单,声明一个变量并在变量中进行查询。$query = "SELECT * FROM text WHERE type = ''";if($_GET['l'] == 1){    $query .= "  AND gg = '1'";} elseif($_GET['l'] == 2){    $query .= "  AND gg = '2'";} else {    $query .= " ";}    $num = $db -> query($query) -> num_rows;希望这对你有帮助。
随时随地看视频慕课网APP
我要回答