Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.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 MySQL:使用一个查询从多个表中获取所有关联行?_Php_Mysql_Join_Array Merge - Fatal编程技术网

PHP MySQL:使用一个查询从多个表中获取所有关联行?

PHP MySQL:使用一个查询从多个表中获取所有关联行?,php,mysql,join,array-merge,Php,Mysql,Join,Array Merge,对于菜谱应用程序,我有一个表存储一般菜谱信息和id。然后我有几个其他表存储相关数据,如配料、说明、注释等。目前,我正在使用多个简单的SQL语句获取和连接所有这些数据 但是,我现在需要将整个结果(通过其“recipe_id”与一个配方相关的所有内容)放入一个数组中,以便可以将其作为一个实体进行操作 我首先尝试了array\u merge,但后来继续使用联接,但我不确定它们是否符合我的要求。那是我要走的路线吗还有其他选择吗 这是我目前的代码: $conn = connDB(); // Get

对于菜谱应用程序,我有一个表存储一般菜谱信息和id。然后我有几个其他表存储相关数据,如配料、说明、注释等。目前,我正在使用多个简单的SQL语句获取和连接所有这些数据

但是,我现在需要将整个结果(通过其“recipe_id”与一个配方相关的所有内容)放入一个数组中,以便可以将其作为一个实体进行操作

我首先尝试了
array\u merge
,但后来继续使用联接,但我不确定它们是否符合我的要求。那是我要走的路线吗还有其他选择吗

这是我目前的代码:

$conn = connDB();
    // Get basic recipe data
    $sql = "SELECT recipe_id, date_created, name, description, author, cooktime, preptime, totaltime, yield, category_id, cuisine_id, image, image_url, url FROM recipes WHERE recipe_id=" . $recipe_id . " LIMIT 1";
    $result = $conn->query($sql);

    // Check that we get a result - ie a valid recipe_id
    if ($result->num_rows > 0) {
        $basicrow = $result->fetch_assoc(); 

        // Get ingredients
        $sql = "SELECT recipe_id, ingredient_id, ingredient, uom_id, ingredient_quant FROM ingredients WHERE recipe_id=" . $recipe_id . "";
        $ingredientresult = $conn->query($sql);
        $ingredientrow = $ingredientresult->fetch_assoc();  

        // Get Units Of Measurements
        $sql = "SELECT uom_id, uom_long, uom_short FROM uom";
        $uomresult = $conn->query($sql);

        if ($uomresult->num_rows > 0) {
            $uomArray = array();
            while($uomrow = $uomresult->fetch_assoc()) {
                $uomArray[$uomrow['uom_id']]["uom_id"] = $uomrow['uom_id'];
                $uomArray[$uomrow['uom_id']]["uom_long"] = $uomrow['uom_long'];
                $uomArray[$uomrow['uom_id']]["uom_short"] = $uomrow['uom_short'];
            }
        }

        // Get instructions
        $sql = "SELECT recipe_id, instruction_id, instruction FROM instructions WHERE recipe_id=" . $recipe_id . "";
        $instructionresult = $conn->query($sql);

        // Get notes
        $sql = "SELECT recipe_id, date_created, note FROM notes WHERE recipe_id=" . $recipe_id . "";
        $notesresult = $conn->query($sql);

    } else {
        echo "No such recipe"; // Not a valid recipe_id
    }

    $conn->close();

您可以创建与配方id无关的关联数组,然后可以循环遍历它们并以所需的形式组合信息。使用联接是在一个数组中获取此数据的最佳方法。关于这一点,我唯一要注意的是查询运行所需的时间。我个人使用了这两种方法,这完全取决于我正在尝试做什么,以及加载相关页面所需的时间

你听说过table joins吗?我的朋友?Matt问题是“然后继续使用joins,但我不确定它们是否符合我的要求”,显然他听说过。
JOIN
确实是这样做的。如果他们不做你喜欢的事,那么你可能做得不对。但是,由于您没有展示您尝试的内容,因此无法帮助您更正它。您的代码只允许配方中的一种成分。这似乎不对。如果将所有查询合并到一个联接中,就会得到大量重复的结果,因为联接会产生叉积。因此,如果一个食谱有5种配料和3种注释,你会得到15行,每种配料和注释的组合一行。是的,这就是我现在正在玩弄的想法。使用我现在拥有的结果集数组(已经获得了我想要的数据)并在其中循环创建下一步所需的嵌套数组。使用联接不会为这样的查询创建大量重复结果吗(如上所述)?每个食谱可以有很多成分,也可以只有很少的成分。多个指令或无指令等等。联接不应创建任何人的记录。它可能会复制数据。这道菜的名字是什么。但这应该很容易处理。