nayanm Posted February 26, 2015 Share Posted February 26, 2015 Hi all, I am working on a search script which searches article titles from the table. i have worked out the function, but have not been able to add an error message if the results are zero. my class file has this following function public function search($table){ $search=$_GET['search']; if ($this->databaseConnection()) { $sql="SELECT * FROM $table WHERE title LIKE '%$search%'"; $q = $this->db_connection->query($sql) or die("failed!"); while($r = $q->fetch(PDO::FETCH_ASSOC)){ $data[]=$r; } return $data; } } my results page has the following code <?php foreach($crud->search("articles") as $value){ extract($value); echo <<<show <p>" <a href="view.php?article_id=$article_id">$title</a> "</p> <br> show; } ?> would be highly obliged if anyone can help me out. apologies if this is a stupid question, but i am pretty much an amateur still. regards, Nayan Quote Link to comment https://forums.phpfreaks.com/topic/294912-add-error-message/ Share on other sites More sharing options...
joel24 Posted February 26, 2015 Share Posted February 26, 2015 (edited) What is db_connection? Is it an instance of the PDO db link? http://php.net/manual/en/pdostatement.rowcount.php You should be able to use num_rows, i.e. if ($q->fetchColumn()==0) //throw error here Edited February 26, 2015 by joel24 Quote Link to comment https://forums.phpfreaks.com/topic/294912-add-error-message/#findComment-1506799 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.