php - Trying to convert mysqli code to pdo -
i have mysqli code , trying convert mysqli pdo. have attempted below having trouble converting all. can please?
<?php require_once './config.php'; include './header.php'; include('db.php'); include('database.php'); if($_post) { mysqli_real_escape_string($connection,$_post['amount']); $values = str_replace(' ','',$_post['amount']); $values = str_replace('£','',$values); $values = explode('-',$values); $min = $values[0]; $max = $values[1]; $res = mysqli_query($connection,'select `image`,`recipe_name`,`recipe_price` recipe `recipe_price` between "'.$min.'" , "'.$max.'"'); $count = mysqli_num_rows($res); $html=''; if($count > 0) { while($row=mysqli_fetch_array($res)) { $image = $row['image']; $recipe_name = $row['recipe_name']; $recipe_price = $row['recipe_price']; $html .= '<div>'; $html .= '<br /><img src=images/'.$image.">"; $html .= 'name: '.$recipe_name; $html .= '<br />price: '.$recipe_price; $html .= '</div><br /><hr />'; } } else { $html='no recipes found'; } } else { $min = 0; $max = 10; $html=''; } ?> could , tell me going wrong in code below?
require_once './config.php'; include './header.php'; include('db.php'); include('database.php'); if($_post && isset($_post['amount'])){ $values = $_post['amount']; $values = ['£', '', $values]; $values = explode('-',$values); $min = $values[0]; $max = $values[1]; $sql = "select `recipe_name`, `recipe_price`, `image` `recipe` `recipe_price` between "'.$min.'" , "'.$max.'""); $count = $stmt->fetchall(); if ($count->fetchcolumn() > 0) { } while($row = $stmt->fetch(/* pdo::fetch_assoc */)) { // loop stuff } echo $row['recipe']; { } else { } else { $min = 0; $max = 10; $html=''; } ?>
you need call prepare , execute perform query. had mistakes in how set $min , $max, rewrote unnecessarily.
$values = str_replace(array(' ', '£'), '', $_post['amount']); list($min, $max) = explode('-', $values); $sql = "select `recipe_name`, `recipe_price`, `image` `recipe` `recipe_price` between :min , :max"); $stmt = $connection->prepare($sql); $stmt->execute(array(':min' => $min, ':max' => $max)); $rows = $stmt->fetchall(pdo::fetch_assoc); if (count($rows) > 0) { foreach ($rows $row) { // loop stuff } } else { $min = 0; $max = 10; $html = ''; }
Comments
Post a Comment