Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 选择类别包括所有父项的产品_Php_Mysqli - Fatal编程技术网

Php 选择类别包括所有父项的产品

Php 选择类别包括所有父项的产品,php,mysqli,Php,Mysqli,我的数据库中有两个表: create table category (id_category integer, name_category text, parent_id integer); create table product (id_product integer, name_product text, id_category integer, description text); insert into category values (1, 'Cat

我的数据库中有两个表:

create table category (id_category integer, name_category text, parent_id integer);
create table product (id_product integer, name_product text, id_category integer, description text);

insert into category
        values
        (1, 'Category A', null),
        (2, 'Category B', null),
        (3, 'Category C', null),
        (4, 'Category D', null),
        (5, 'Subcategory Of 1', 1),
        (6, 'Subcategory Of 5', 5),
        (7, 'Subcategory Of 5', 5),
        (8, 'Subcategory of D', 4)
        ;

insert into product
        values
        (1, 'Product One', 5, 'Our first product'),
        (2, 'Product Two', 6, 'Our second product'),
        (3, 'Product three', 6, 'Our third product'),
        (4, 'Product four', 6, 'Our 4th product'),
        (5, 'Product five', 7, 'Our 5th product'),
        (6, 'Product six', 8, 'The even better one');
我怎样才能像这样返回:

id_category| name_product | id_product    |               
-----------+--------------+---------------+
         1 | Product One  |             1 |
         1 | Product Two  |             2 |
         1 | product three|             3 |
         1 | Product four |             4 |
         1 | Product five |             5 |
最好的方法是什么

稍后我将从php中获取id\u category实例变量id\u category=1 我可以使用sql查询还是必须使用php函数


我想创建一些类似于电子商务的产品,它们有几个分层类别(树),当客户单击父类别时,输出所有带有父类别的产品


请原谅我一个不好的问题,因为我使用google translate

您可以像这样使用SQL查询:
SELECT*FROM product WHERE id\u category=1


你看到WHERE语句了吗?这是您可以设置要筛选的内容的地方。

我想像一些电子商务一样创建,在那里它们有几个分层类别(树),当客户单击父类别时,输出所有产品parents@jokowi我给你的查询是你所需要的,当然只需要很少的编辑。如果您对PHP不够熟悉,我建议您阅读一些关于如何使用PHP从SQL中获取数据的文章。我想您可能误解了,id_category是从表类别中获取的,当然必须使用join语句,但我也希望从child获得输出。您想只要求正确的查询来实现这一点吗?或者php进程完成了获取数据的查询?我只需要查询sql