Php和MySQL从多个表中读取数据

Php和MySQL从多个表中读取数据,php,mysqli,Php,Mysqli,这个代码有什么问题。我有3个表,我正试图从中获取数据。配方表、配料表和接收配料表。RecipeInResient保存配方的ID和配方的配料。到目前为止,我可以显示配方和ReceivingCredit表中的数据。现在我试图从配料表中获取数据 $id = $_GET['id'] ?? ''; //PHP > 7.0 $recipe_id = $id; $recipe = find_recipe_by_id($id); $recipeingredient_set = find_all_re

这个代码有什么问题。我有3个表,我正试图从中获取数据。配方表、配料表和接收配料表。RecipeInResient保存配方的ID和配方的配料。到目前为止,我可以显示配方和ReceivingCredit表中的数据。现在我试图从配料表中获取数据

$id = $_GET['id'] ?? ''; //PHP > 7.0
$recipe_id = $id;   
$recipe = find_recipe_by_id($id);
$recipeingredient_set = find_all_recipeingredient_by_recipe_id($recipe_id);

while($recipeingredient = mysqli_fetch_assoc($recipeingredient_set)){
        $ingredient = find_ingredient_by_id($recipeingredient['ingredient_id']);
            echo "<br />    ";
            echo $ingredient['name'];   
            echo "<br />    ";

    } 
    function find_ingredient_by_id($id){
    global $db;

    $sql = "SELECT * FROM Ingredient ";
    $sql .= "WHERE id='" . $id . "'";
    $result = mysqli_query($db, $sql);
    confirm_result_set($result);
    $ingredient = mysqli_fetch_assoc($result);

    return $result; // returns an assoc. array

}
function find_all_recipeingredient_by_recipe_id($recipe_id){
    global $db;

    $sql = "SELECT * FROM RecipeIngredient ";
    $sql .= "WHERE recipe_id='" . $recipe_id . "'";
    $result = mysqli_query($db, $sql);
    confirm_result_set($result);
    return $result;
}   
$id=$\u GET['id']??“”//PHP>7.0
$recipe_id=$id;
$recipe=通过\u id($id)查找\u recipe\u;
$recipeingredient\u set=按配方id($recipe\u id)查找所有配方;
而($RecipeIngCredit=mysqli_fetch_assoc($RecipeIngCredit_set)){
$MENTORY=按id查找配料($RecipeCredit['MENTORY\U id');
回声“
”; echo$成分['name']; 回声“
”; } 函数按\u id($id)查找\u成分{ 全球$db; $sql=“从配料中选择*”; $sql.=“其中id=”。“$id.”; $result=mysqli_查询($db,$sql); 确认结果集($result); $component=mysqli\u fetch\u assoc($result); return$result;//返回一个关联数组 } 函数按配方id(“配方id”)查找所有配方用户{ 全球$db; $sql=“从RecipeingCredit中选择*”; $sql.=“其中配方id=”。$recipe\u id。“”; $result=mysqli_查询($db,$sql); 确认结果集($result); 返回$result; }
您返回的是结果而不是数组,只需更新返回行即可

 function find_ingredient_by_id($id){
global $db;

$sql = "SELECT * FROM Ingredient ";
$sql .= "WHERE id='" . $id . "'";
$result = mysqli_query($db, $sql);
confirm_result_set($result);
$ingredient = mysqli_fetch_assoc($result);

return $ingredient; // returns an assoc. array
}

您正在返回resultset
return$result;//返回关联数组
,但应返回关联数组$component

    function find_ingredient_by_id($id){
        global $db;

        $sql = "SELECT * FROM Ingredient ";
        $sql .= "WHERE id='" . $id . "'";
        $result = mysqli_query($db, $sql);             //The resultset. This is what you were returning.
        confirm_result_set($result);
        $ingredient = mysqli_fetch_assoc($result);     //An assoc array. What you wanted to return.

        return $ingredient; // returns an assoc. array

    }

你桌子的结构是什么?@jeff我有照片,但不能上传照片。因为这里的人讨厌我,因为我有诵读困难症,不能正确解释问题和打字,我被否决了,只需发布create table语句。谢谢你在这方面的帮助。它工作了,我还设法输入了测量数据和数量,以便在CRUD工作中为R完成孔视图配方页面。查看