php - 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?
解决方案
如果您想要两个表中的所有数据,请删除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']
.
推荐阅读
- postgresql - 无法将 XXX 转换为 Varbit
- youtube-api - Youtube API Statuscode 403“调用者没有权限”即使我有权限
- azure-cosmosdb - CosmosDB 空间索引创建 - 错误?
- c# - System.Resources.ResXResource[X] 与 Blazor
- node.js - NodeJS 应用程序可以在本地运行,但不能在 EC2 上运行 - NodeJS
- vim - 如何只为映射vim函数的结果设置颜色?
- sql - Informix SQL - Number of days between 2 dates
- reactjs - 如何在 Ionic React 的 IonSearchbar 中将取消按钮移动到右侧
- python - 解决sympy中两个块矩阵之间的符号方程组?
- api - 实体框架递归连接