I created a simple search system, but when it goes show the search results, it doesn’t work because it keeps in infinite loop and show only the first line the table. What it takes to show several results using the structure below (PDO): if(!empty($_POST['nome'])) { $query = " SELECT * FROM np_outdoor WHERE nome LIKE :nome ORDER BY nome DESC "; $query_params = array( ':nome' => '%'.$_POST['nome'].'%' ); try { $stmt = $db->prepare($query); $result = $stmt->execute($query_params); } catch(PDOException $ex) { die("Failed to run query: " . $ex->getMessage()); } $row = $stmt->fetch(PDO::FETCH_ASSOC); } if ($row == '0') { echo("nothing"); } else while ($line = $row) { echo $line['nome']; } PHP: I did it !!!
Uhm... you're not fetching a count, you're fetching an entire row as an ARRAY, so that ==0 makes no sense... nor does your WHILE statement make any sense since you're just copying the already copied row to $line -- that shouldn't even do anything. What I THINK you are trying to do is this: if (!empty($_POST['nome'])) try { $statement = $db->prepare(' SELECT * from np_outdoor WHERE nome LIKE :nome ORDER BY nome DESC '); $result = $statement->execute(array( ':nome' => '%' . $_POST['nome'] . '%' )); } catch(PDOException $e) { die('Failed to run query: ' . $e->getMessage()); } if ($row = $statement->fetch()) { do { echo $row['nome']; } while ($row = $statement->fetch()); } else echo 'nothing'; Code (markup): Notice I ditched all the unnecessary variables which just wasted memory for nothing.