I have implemented a search box in my website, which worked perfectly until i decided to protect against sql injection. I decided to go with PDO as many people have suggested that its better than using mysql_string_escape. when i try to fetch data from my database it returns nothing, but i know there are records in there.
My code
db.php
$mysqli = new mysqli("localhost","root", "", "1000_AD");
result.php
<?php
if (isset($_GET['search'])){
$search_query = $_GET['user_query'];
$get_foo = $mysqli->prepare("SELECT * FROM `food` WHERE `food_keywords`
LIKE ?");
$search_query = "%".$search_query."%";
$get_foo->bind_param('s',$search_query);
$get_foo->execute();
$obj = $get_foo->fetch();
if($obj==0){
echo "<h4>No results where found!</h4>";
}
//fetch results set as object and output HTML
while($list = $get_foo->fetch())
{
$foo_id = $list['food_id'];
echo '<div class="food">';
echo '<form method="post" action="basket.php" id = "add-basket-form"
>';
echo '<h4>'.$list['food_title'].'</h4>';
echo '<div class="pic"><img src="admin/food_images/'.$list['food_image'].'"
width= "180" height= "160"></div>';
echo "<p><b>£".$list['food_price']."</b></p>";
echo '<div class="sp-quantity">
<div class="sp-minus fff"> <a class="ddd">-</a>
</div><div class="sp-input"> <input type="text" name= "product_qty" class
="quntity-input" value="1" /></div>
<div class="sp-plus fff"> <a class="ddd">+</a> </div></div>';
echo '<div class = "btn"><a href="info.php?foo_id='.$list['food_id'].'"
style="float:left">INFO</a></div>';
echo '<div class = "add_b"><button>Add to Basket</button></div>';
echo '<input type="hidden" name="food_code"
value="'.$list['food_id'].'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '</form>';
echo '</div>';
}
}
?>
it successfully gets the number of rows, because it echo's the buttons twice to show two records where found, which is correct. The problem is..it doesn't fetch the title,image or price from the database. i've tried many suggestions such as
while($list = $get_foo->fetchAll(PDO::FETCH_ASSOC))
i get the error undefined method fetch all, i discovered that you need to install a driver for fetchAll to work. which i dont want to do. How can i correctly fetch data from my database?