首页 > 解决方案 > PHP & SQL LEFT JOIN when i echo it display only content with id 1

问题描述

So basically, I have 2 tables first table called "brand" and it contains columns "brand_id", "brand_name" Then I have a second table called "products" that contains many columns but the most important is "product_brand"
Brand table:

Product table:

When i use this code

$brand = mysqli_query($conn, "SELECT products.*,brand.brand_name FROM products LEFT JOIN brand ON products.product_brand = brand.brand_id")  or die(mysqli_errno($conn). '-'. mysqli_error($conn));

then i try to echo it like this <?php echo $znacka['brand_name']; ?>

When i do that it only display the first brand on every product.

Can you please give me some advice?

标签: phpsqlmysqlileft-join

解决方案


如果您想要两个表中的所有数据,请删除left。您必须为此应用内部联接。

$brand = mysqli_query($conn, "SELECT products.*,brand.brand_name FROM products JOIN brand ON products.product_brand = brand.brand_id")  or die(mysqli_errno($conn). '-'. mysqli_error($conn));

现在您可以使用$znacka['brand_name'].


推荐阅读