简体   繁体   English

MySQL查询结果来自多个条件

[英]Mysql query result from multiple conditions

To anyone trying to do the same: 对于任何尝试这样做的人:

This code respond to multiple HTML selects, and if the user press the button without filters, it displays a SELECT *. 该代码响应多个HTML选择,并且如果用户按下不带过滤器的按钮,它将显示SELECT *。

Victor Koenders fixed the query, thank you. Victor Koenders解决了该查询,谢谢。

As LPChip pointed out, the foreach needs two diferent variables, one that calls the function and the other that shows the table columns on a table. 正如LPChip所指出的,foreach需要两个不同的变量,一个变量调用函数,另一个变量显示表中的表列。

The simple cuotes on the columns did not work, only when I remove them it worked, as in SELECT * FROM table WHERE variable = ´criteria´. 列上的简单提示不起作用,仅当我将其删除时才起作用,如SELECT * FROM table WHERE variable =“ criteria”中所示。

I marked LPChip as the answer because that damned foreach was keeping me up. 我将LPChip标记为答案,因为那该死的foreach使我无法自拔。

I hope this helps somebody here, this site is awesome. 我希望这对这里的人有所帮助,这个网站很棒。

public function filterFood() {

    $variable1 = $_POST['variable1'];   
    $variable2 = $_POST['variable2'];
    $variable3 = $_POST['variable3'];
    $criteria1 = $_POST['criteria1'];   
    $criteria2 = $_POST['criteria2'];
    $criteria3 = $_POST['criteria3'];

    $this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    $query = "SELECT * FROM food";
    $conditions = array();

    if($criteria1 !="") {
      $conditions[] = $variable1." = '".$criteria1."'";
    }
    if($criteria2 !="") {
      $conditions[] = $variable2." = '".$criteria2."'";
    }
    if($criteria3 !="") {
      $conditions[] = $variable3." = '".$criteria3."'";
    }

    $sql = $query;
    if (count($conditions) > 0) {
      $sql .= " WHERE " . implode(' AND ', $conditions);
    }

    //$result $this->db_connection->query($sql);
    return $this->db_connection->query($sql);

    var_dump($sql);
}

And: 和:

<?php

foreach($filter_food->filterFood() as $filtered_food) {
?>

Now I can finally go to sleep. 现在我终于可以睡觉了。

$result = $this->db_connection->$sql;

这是您的问题,您想做类似的事情

$result = $this->db_connection->query($sql);
foreach($filter_food->filterFood() as $filter_food)

This will never work, because you are using array $filter_food, and then you are making each result $filter_food which causes a backloop, and thus it won't work. 这永远都行不通,因为您使用的是数组$ filter_food,然后使每个结果$ filter_food都导致回环,因此它将不起作用。

You could change the as $filter_food to $filtered_food or anything else to make it work. 您可以将as $ filter_food更改为$ filtered_food或其他任何方式使其起作用。

Also, I usually write my querys like this: any column or table I use the `` quotes, and any variable I use the "..." This could solve your query property problem. 另外,我通常这样编写查询:我使用``引号引起的任何列或表,而我使用“ ...”引起的任何变量都可以解决您的查询属性问题。 Especially quoting the tables, as it might see one of the tables as a function. 尤其是引用表,因为它可能会将表之一视为函数。

Your query would become: 您的查询将变为:

SELECT * FROM `food` WHERE `container_food` = "SACO" AND `animal_name_food` = "GATO"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM