我正在尝试将mysqli转换为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"];
我的尝试没有用,我的问题是我不确定mysqli_num_rows与PDO是什么等效:输出应获得相似类别列表之间的产品,以显示为电子商务网站的相关产品
$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 "
感谢任何人都可以提供帮助,我仍在尝试学习php和mysql,但我已经阅读到数据库的PDO功能比mysqli更好。
实际上,您也不需要为mysqli计算行数。这个功能没用。
首先,选择所有数据作为数组。
$sql = "SELECT * FROM product WHERE id BETWEEN ? AND ?";
// There is likely a problem in your SQL so I removed the `prd_cat = cat_id ` part
$stmt = $connect->prepare($sql);
$stmt->execute([$_GET['p'], $_GET['p'] + 3]);
$data = $stmt->fetchAll();
然后简单地遍历此数组:
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"];
}
$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"];
}
}