Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.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_Mysql_List - Fatal编程技术网

Php 从表中输出多个结果

Php 从表中输出多个结果,php,mysql,list,Php,Mysql,List,继续 类别 cid | category_name 1 | desserts 2 | cakes 3 | biscuits id | recipe_name 1 | black forest cake 2 | angel cake 3 | melting moments 4 | croquembouche 5 | crepes suzette iid | ingredient_code | ingredient_name | ingredient_brand

继续

类别

cid | category_name
 1  | desserts
 2  | cakes
 3  | biscuits
id | recipe_name
1  | black forest cake
2  | angel cake
3  | melting moments
4  | croquembouche
5  | crepes suzette
iid | ingredient_code | ingredient_name      | ingredient_brand
 1  |     abc201      | self-raising flour   |    white wings
 2  |     abc202      | milk                 |    pura
 3  |     abc203      | chocolate            |    plaistock
 4  |     abc204      | baking powder        |    mackenzie
 5  |     abc205      | plain flour          |    white wings
 6  |     abc206      | eggs                 |    free range
recipe_id | category_id
   1      |    1
   4      |    1
   5      |    1
   1      |    2
   2      |    2
   3      |    3
   4      |    3
recipe_id | ingredient_id
   1      |    1
   2      |    1
   4      |    1
   1      |    2
   2      |    2
   3      |    2
   5      |    2
   1      |    3
   2      |    3
   1      |    4
   3      |    5
   4      |    5
食谱

cid | category_name
 1  | desserts
 2  | cakes
 3  | biscuits
id | recipe_name
1  | black forest cake
2  | angel cake
3  | melting moments
4  | croquembouche
5  | crepes suzette
iid | ingredient_code | ingredient_name      | ingredient_brand
 1  |     abc201      | self-raising flour   |    white wings
 2  |     abc202      | milk                 |    pura
 3  |     abc203      | chocolate            |    plaistock
 4  |     abc204      | baking powder        |    mackenzie
 5  |     abc205      | plain flour          |    white wings
 6  |     abc206      | eggs                 |    free range
recipe_id | category_id
   1      |    1
   4      |    1
   5      |    1
   1      |    2
   2      |    2
   3      |    3
   4      |    3
recipe_id | ingredient_id
   1      |    1
   2      |    1
   4      |    1
   1      |    2
   2      |    2
   3      |    2
   5      |    2
   1      |    3
   2      |    3
   1      |    4
   3      |    5
   4      |    5
成分

cid | category_name
 1  | desserts
 2  | cakes
 3  | biscuits
id | recipe_name
1  | black forest cake
2  | angel cake
3  | melting moments
4  | croquembouche
5  | crepes suzette
iid | ingredient_code | ingredient_name      | ingredient_brand
 1  |     abc201      | self-raising flour   |    white wings
 2  |     abc202      | milk                 |    pura
 3  |     abc203      | chocolate            |    plaistock
 4  |     abc204      | baking powder        |    mackenzie
 5  |     abc205      | plain flour          |    white wings
 6  |     abc206      | eggs                 |    free range
recipe_id | category_id
   1      |    1
   4      |    1
   5      |    1
   1      |    2
   2      |    2
   3      |    3
   4      |    3
recipe_id | ingredient_id
   1      |    1
   2      |    1
   4      |    1
   1      |    2
   2      |    2
   3      |    2
   5      |    2
   1      |    3
   2      |    3
   1      |    4
   3      |    5
   4      |    5
配方\u类别

cid | category_name
 1  | desserts
 2  | cakes
 3  | biscuits
id | recipe_name
1  | black forest cake
2  | angel cake
3  | melting moments
4  | croquembouche
5  | crepes suzette
iid | ingredient_code | ingredient_name      | ingredient_brand
 1  |     abc201      | self-raising flour   |    white wings
 2  |     abc202      | milk                 |    pura
 3  |     abc203      | chocolate            |    plaistock
 4  |     abc204      | baking powder        |    mackenzie
 5  |     abc205      | plain flour          |    white wings
 6  |     abc206      | eggs                 |    free range
recipe_id | category_id
   1      |    1
   4      |    1
   5      |    1
   1      |    2
   2      |    2
   3      |    3
   4      |    3
recipe_id | ingredient_id
   1      |    1
   2      |    1
   4      |    1
   1      |    2
   2      |    2
   3      |    2
   5      |    2
   1      |    3
   2      |    3
   1      |    4
   3      |    5
   4      |    5
配方\u配料

cid | category_name
 1  | desserts
 2  | cakes
 3  | biscuits
id | recipe_name
1  | black forest cake
2  | angel cake
3  | melting moments
4  | croquembouche
5  | crepes suzette
iid | ingredient_code | ingredient_name      | ingredient_brand
 1  |     abc201      | self-raising flour   |    white wings
 2  |     abc202      | milk                 |    pura
 3  |     abc203      | chocolate            |    plaistock
 4  |     abc204      | baking powder        |    mackenzie
 5  |     abc205      | plain flour          |    white wings
 6  |     abc206      | eggs                 |    free range
recipe_id | category_id
   1      |    1
   4      |    1
   5      |    1
   1      |    2
   2      |    2
   3      |    3
   4      |    3
recipe_id | ingredient_id
   1      |    1
   2      |    1
   4      |    1
   1      |    2
   2      |    2
   3      |    2
   5      |    2
   1      |    3
   2      |    3
   1      |    4
   3      |    5
   4      |    5
正如Ozantomic所建议的,我使用以下查询:

SELECT A.recipe_name, GROUP_CONCAT(ingredient_name) AS ingredient_names
FROM recipes A
LEFT JOIN recipe_ingredients B ON A.id = B.recipe_id
LEFT JOIN ingredients C ON B.ingredient_id = C.iid
LEFT JOIN recipe_categories D ON A.id = D.recipe_id
LEFT JOIN categories E ON D.category_id = E.cid
WHERE category_id = <search_id>
GROUP BY id
我觉得要么使用两个查询来实现这一点,要么使用foreach循环。哪一个最好?我该怎么做

编辑:我还需要能够格式化的结果,使用html标记或放入div或任何东西

仍然适用以下条件:


您确实需要将一个列表放在一起,并在其上循环,以提取您要查找的确切数据。

谢谢Charles:-)但是当我进入“到目前为止有没有我?”之前的步骤时,输出会重复3次,所以我的结局是:黑森林蛋糕黑森林蛋糕薄饼苏赛特薄饼苏赛特薄饼苏赛特薄饼苏赛特薄饼!SQL处理行和列。每个食谱都有多行配料。将表连接在一起时,如果后面的表中有多个匹配行,则某些列将重复。这就是为什么我在最后将循环中的配方信息手动组合在一起的原因。另外,对于PDO,无论使用哪个数据库接口,所提供的一般概念(准备、执行、获取)都应该是相同的。您应该使用PDO或mysqli来支持mysql。我使用的是phpmyadmin。我不想为结果创建一个数组——如果有意义的话,我想单独使用结果。我想能够使用html标签的每一个成分,无论是一个类或一个href到一个单独的页面专用于该成分。两周前我从未使用过mysql。这是一条极其陡峭的学习曲线。