[英]How can I Fetch data from database from different tables at the same time PHP MYSQL
I have 2 tables that are related to one another.我有 2 个相互关联的表。 The first table is a product table which includes (Product_id,product_title and product_price) and the other one is images table which includes (image_id, image_product_title(FK) and image_name).第一个表是包含(Product_id,product_title 和 product_price)的产品表,另一个是包含(image_id,image_product_title(FK)和image_name)的图像表。 So here I am trying to fetch all products with its images so it will have a product and then inside that product multiple images of a product but I am having trouble calling 2 different queries in a nested way.所以在这里我试图获取所有产品及其图像,这样它就会有一个产品,然后在该产品内部有一个产品的多个图像,但我无法以嵌套方式调用 2 个不同的查询。 Here is my code so you can understand what I am trying to do...这是我的代码,因此您可以了解我要做什么...
$stmt = mysqli_prepare($connection,"SELECT product_id,product_title,product_price FROM products");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt,$product_id,$product_title,$product_price);
while(mysqli_stmt_fetch($stmt)){
//HTML PART
$stmt2 = mysqli_prepare($connection,"SELECT image_id,image_product_title,image_name FROM images WHERE image_product_title = '$product_title'");
mysqli_stmt_execute($stmt2);
mysqli_stmt_bind_result($stmt2,$image_id,$image_product_title,$image_name);
while(mysqli_stmt_fetch($stmt2)){
echo $image_name;
}
//HTML PART
echo $product_id;
echo $product_title;
echo $product_price;
//HTML PART
}
Is there any way to do something like this?有没有办法做这样的事情? The main idea is to loop all products and inside that loop second loop will loop actual products images but product is not at the same table with images so I am struggling to take datas from both tables in the nested way主要思想是循环所有产品,在该循环内第二个循环将循环实际产品图像,但产品与图像不在同一个表中,所以我很难以嵌套方式从两个表中获取数据
Don't mix PHP and HTML.不要混合 PHP 和 HTML。 First prepare the data in PHP and then only display it in HTML.首先准备PHP中的数据,然后只在HTML中显示。
Generally, this kind of thing would be much simpler to do with PDO.一般来说,这种事情用 PDO 会简单得多。 If it's not too late consider switching to PDO.如果还不算太晚,请考虑切换到 PDO。
To solve the problem you have to use buffered results or fetch everything into an array/object before executing the next statement.要解决这个问题,您必须在执行下一条语句之前使用缓冲结果或将所有内容提取到数组/对象中。 If you must use mysqli then something like this should do the job:如果您必须使用 mysqli 那么这样的事情应该可以完成:
// preparation of a nested array structure with products and their images
$stmt = $connection->prepare('SELECT product_id,product_title,product_price FROM products');
$stmt->execute();
$products = [];
foreach ($stmt->get_result() as $row) {
$products[$row['product_id']] = $row;
}
$stmt = $connection->prepare('SELECT image_id,image_product_title,image_name FROM images');
$stmt->execute();
foreach ($stmt->get_result() as $image) {
$products[$image['product_id']][] = $image;
}
Then outputting this in HTML is a piece of cake:然后在 HTML 中输出这个是小菜一碟:
<?php foreach($products as $product): ?>
<?=htmlspecialchars($product['product_title']) ?>
<?php foreach($products['images'] as $image): ?>
<?=htmlspecialchars($image['image_name']) ?>
<?php endforeach ?>
<?php endforeach ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.