简体   繁体   中英

PHP-Mysql WHERE is empty AND returns results

I am not well versed in MYSQL, and I need some guidance here. I have read multiple MYSQL WHERE statement explanations, but have not found this exact issue. I have a table called studies that has fields like id, name, author, category, format and a few others.

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? 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:

SELECT * FROM estudos WHERE format='$format'

Alternatively, use OR instead of 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).

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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