Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple conditions queries with PDO
    text
    copied!<p>I asked this question in <a href="http://codereview.stackexchange.com">http://codereview.stackexchange.com</a> and they wanted me to post it here. I couldn't get this code to work at all. I switched from regular mysql to pdo which is more safer. Could someone tell me what I'm missing here. I've been struggling with it for couple of day, and I could find exact answer when I first searched this site. </p> <pre><code> $input = $_POST['input']; $categories = $_POST['category']; $state = $_POST['state']; $zipcode = $_POST['zipcode']; $qq = $db-&gt;prepare(" SELECT * FROM classified ")or die(print_r($qq-&gt;errorInfo(), true)); /*** execute the prepared statement ***/ $qq-&gt;execute(); /*** echo number of columns ***/ $rows = $qq-&gt;fetch(PDO::FETCH_NUM); if ($rows&gt;0){ $query = (" SELECT * FROM classified "); $cond = array(); $params = array(); if (!empty($input)) { $cond[] = "title = ?"; $params[] = $input; } if (!empty($categories)) { $cond[] = "id_cat = ?"; $params[] = $categories; } if (!empty($state)) { $cond[] = "id_state = ?"; $params[] = $state; } if (!empty($zipcode)) { $cond[] = "zipcode = ?"; $params[] = $zipcode; } if (count($cond)) { $query .= ' WHERE ' . implode(' AND ', $cond)or die(print_r($query-&gt;errorInfo(),true)); } $stmt = $db-&gt;prepare($query); $stmt-&gt;execute($params); $ro = $stmt-&gt;fetch(PDO::FETCH_NUM); } if ($ro &gt; 0) { foreach ($stmt-&gt;fetchAll(PDO::FETCH_ASSOC) as $row) { echo $row['title']; echo $row['categories']; echo $row['state']; echo $row['zipcode']; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload