简体   繁体   English

PHP-Mysql WHERE为空并返回结果

[英]PHP-Mysql WHERE is empty AND returns results

I am not well versed in MYSQL, and I need some guidance here. 我不熟悉MYSQL,我需要一些指导。 I have read multiple MYSQL WHERE statement explanations, but have not found this exact issue. 我已阅读多个MYSQL WHERE语句解释,但未发现此确切问题。 I have a table called studies that has fields like id, name, author, category, format and a few others. 我有一个名为研究的表,其中包含id,名称,作者,类别,格式和其他一些字段。

I am building a filter with checkboxes to narrow down the query. 我正在构建一个带有复选框的过滤器来缩小查询范围。 I want to get all studies with a specific author AND category AND format. 我想用特定的作者和类别和格式进行所有研究。 My problem comes when I build the query. 我的问题出现在我构建查询时。 If only the format is selected, the query returns empty because no author was selected. 如果仅选择格式,则查询将返回空,因为未选择任何作者。 How do I get the results from the AND statements without the WHERE statement? 如何在没有WHERE语句的情况下从AND语句中获取结果? Here is what I have so far... 这是我到目前为止所拥有的......

"SELECT * FROM estudos WHERE subcategory='$subcategory' AND author='$author' AND format='$format'"

If author is empty, don't include it in the query: 如果author为空,请不要在查询中包含它:

SELECT * FROM estudos WHERE format='$format'

Alternatively, use OR instead of AND: 或者,使用OR而不是AND:

SELECT * FROM estudos WHERE subcategory='$subcategory' OR author='$author' OR format='$format'

You need to conditionally append the conditions based on whether the item was selected or not. 您需要根据项目是否被选中有条件地附加条件。 I suggest something like: 我建议像:

$where = array();
$params = array();
if (isset($format)) {
    $where[] = "format = ?";
    $params[] = $format;
}
/* other parameters */
$query = "SELECT * FROM estudos WHERE "
    . implode(' AND ', $where);

Note that the $params part is only necessary if you are using a parameterized query like with PDO (which you should be doing). 请注意, $params部分仅在您使用PDO参数化查询时才需要(您应该这样做)。

assign where condition to variable 将条件赋值给变量

for example 例如

if($categoryCheckbox){
 $whare.="AND category='$category'";
}

and append it to your query 并将其附加到您的查询

You could use he following code to generate the query 您可以使用以下代码生成查询

$q = "SELECT * FROM estudos WHERE format='$format'";
if ($author != '') { $q.= "AND author='$author'"; }
if ($subcategory != '') { $q.= "AND subcategory='$subcategory'"; }

if only format is selected you should only query based on format like 如果只选择格式,你应该只根据格式查询

SELECT * FROM estudos WHERE format='$format'

if format and author is selected then 如果选择了格式和作者

SELECT * FROM estudos WHERE author='$author' AND format='$format'

if all are selected then 如果全部被选中那么

SELECT * FROM estudos WHERE subcategory='$subcategory' AND author='$author' AND format='$format'

You should run your query depending on the selection 您应该根据选择运行查询

Well what i would do is this: 那么我要做的是:

$query = sprintf("SELECT * FROM estudos");
if ($subcategory != "" || $author != "" || $format != "") {
    $query .= " WHERE ";
    $i = 0;

    if ($subcategory != "") {
        if ($i > 0) $query .= " AND ";
        $query .= "subcategory='" . $subcategory . "'";
    }
    if ($author != "") {
        if ($i > 0) $query .= " AND ";
        $query .= "author='" . $author . "'";
    }
    if ($format != "") {
        if ($i > 0) $query .= " AND ";
        $query .= "format='" . $format . "'";
    }
}

I hope that works. 我希望有效。

I will suggest you to rewrite the query by using concatenation. 我建议你使用串联重写查询。 Like: 喜欢:

$query= 'SELECT * FROM estudos WHERE 1';

if(isset($subcategory)) $query .= ' AND subcategory='.$subcategory;

if(isset($author)) $query .= ' AND author='.$author;

if(isset($format)) $query .= ' AND format='.$format;

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

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