0

I'm trying to convert mysqli to PDO:

$product_id = $_GET['p'];
$product_query = "SELECT * FROM products,categories WHERE product_cat=cat_id AND product_id BETWEEN $product_id AND $product_id+3";
$run_query = mysqli_query($con,$product_query);
      if(mysqli_num_rows($run_query) > 0){
            while($row = mysqli_fetch_array($run_query)){
                        $pro_id    = $row['product_id'];
                        $pro_cat   = $row['product_cat'];
                        $pro_brand = $row['product_brand'];
                        $pro_title = $row['product_title'];
                        $pro_price = $row['product_price'];
                        $pro_image = $row['product_image'];
                        $cat_name = $row["cat_title"];

my attempt was not working, my issue is i'm not sure what is the equivalent of the mysqli_num_rows to PDO: the output should get a a product between a similar category list to show as related products for the ecommerce website

$query = $connect->prepare("SELECT * FROM product WHERE prd_cat = cat_id AND prd_id BETWEEN :product_id AND :product_id+3");
$query->execute(['product_id' => $_GET['p']]);
        while ($row = $query->fetchAll()){
                        $pro_id    = $row['prd_id'];
                        $pro_cat   = $row['prd_cat'];
                        $pro_brand = $row['prd_brand'];
                        $pro_title = $row['prd_name'];
                        $pro_price = $row['prd_price'];
                        $pro_image = $row['prd_image'];

                        $cat_name = $row["cat_title"];

                        echo "

appreciate if anyone can help, I'm still trying to learn php and mysql, but I've read that PDO function to databases is better than mysqli.

user3783243
  • 5,368
  • 5
  • 22
  • 41
  • PDO is better, IMO. There is no row count with `PDO` for `mysql` on a `select`. You can do a count in PHP or run two queries, one with `count(*)`. Additionally emulation is required to used the same placeholder more than once. – user3783243 Oct 23 '19 at 02:46

1 Answers1

-1
$query = $connect->prepare("SELECT * FROM product WHERE prd_cat = cat_id AND prd_id BETWEEN :product_id1 AND :product_id2");
$query->execute(['product_id1' => $_GET['p'], 'product_id2' => ($_GET['p'] + 3)]);
if($query->rowCount() > 0)
{
    $data = $query->fetchAll(\PDO::FETCH_ASSOC);
    foreach($data as $row)
    {
        $pro_id    = $row['prd_id'];
        $pro_cat   = $row['prd_cat'];
        $pro_brand = $row['prd_brand'];
        $pro_title = $row['prd_name'];
        $pro_price = $row['prd_price'];
        $pro_image = $row['prd_image'];

        $cat_name = $row["cat_title"];
    }
}