PHP-Mysql WHERE is empty AND returns results

115

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'"
867

Answer

Solution:

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'
134

Answer

Solution:

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 withPDO (which you should be doing).

578

Answer

Solution:

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

190

Answer

Solution:

assign where condition to variable

for example

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

and append it to your query

387

Answer

Solution:

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'"; }
725

Answer

Solution:

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.

695

Answer

Solution:

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;

People are also looking for solutions to the problem: unix - multiple using openlog php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.