php - 如何连接两个MySQL表

标签 php html mysql

我的 MySqli 数据库中有一个名为“产品”的表。

Products TABLE 
product_id       | INT      primary KEY
product_name     | VARCHAR(50)
product_price    | float

通过 PHP,我在表产品中输入行,如下所示:

   mysqli_query($con,"INSERT INTO products (product_id,product_name,product_price) VALUES 
                                           ('$product_id','$product_name','$price')");                                      

到目前为止,一切都运行良好。现在我想要第二个表称为“类别”,该表将包括产品可以拥有的所有可能的类别

类别表必须有一个category_id和一个category_name作为列

Category TABLE 
category_id       | INT      primary KEY
category_name     | VARCHAR(50)

我正在尝试找到一种将产品与 PHP 文件中的类别连接起来的方法

  for example:
  $get=mysqli_query($con, "SELECT * FROM `category`");
  while ($row = mysqli_fetch_assoc($get)) { 
         echo $row['category_name'];   //...here show all the categories
         //...
         //.. pick the category that the product belong
         $category_Selected= .....;
                    }?>
   ..... And make the connection (with INSERT? or something) between product and category

我希望能够在产品表中进行搜索时(对于产品 X)显示它所属的类别(到目前为止我可以显示 Product_id、product_name 和 Product_price )

谢谢

最佳答案

您想加入您的 table 。

看这里:

Join Tables - w3schools

MySQL Join Tables Syntax

关于php - 如何连接两个MySQL表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54617468/

相关文章:

php - MySQL NOW() 与 PHP 时区不匹配

JavaScript - 将大字符串复制到剪贴板

javascript - 如何不在 <div> 上应用 CSS 文件? (不更改原始 CSS 文件。)

PHP mysql_fetch_assoc 仅返回 1

jquery - 通过复选框刷新 jsp 文件并将结果发布到同一 jsp 文件中,而不转到 servlet?

php - 如果在 php 中满足条件,则将数组对象添加到 foreach 循环中的数组

php - SelectionGroup 不添加额外的属性

MYSQL match against not showing same results as mysql like %% 查询

php - 在php中从数据库中获取字符串仅返回空格之前的部分

HTML5 必需属性两个字段之一