簡體   English   中英

在json中加入多個表

[英]join more than tables in json

我想按json文件顯示數據時遇到問題。 如果我在一個表中顯示數據也可以,但是當我要連接多個表時,沒有數據顯示

<?php
mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname);

 $query = "SELECT Product.Product_Name, Product.Price, Product.Image, Gender.Description, Age.Description, Status.Availability  from Product join Age on Age.Age_ID join Gender on Gender.Gender_ID join Status on Status.ID";

$result = mysql_query($query);

//Create an array
    $json_response = array();

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $row_array['Product_Name'] = $row['Product_Name'];
        $row_array['Price'] = $row['Price'];
        $row_array['Image'] = base64_encode($row["Image"]);
        $row_array['Description'] = $row['Description'];
        $row_array['Description'] = $row['Description'];
        $row_array['Availability'] = $row['Availability'];



        //push the values in the array
        array_push($json_response,$row_array);
    }
    echo json_encode($json_response);

    //Close the database connection
    fclose($db)
?>

您最后一次加入on Status.ID是問題。 Status表沒有ID列。 根據您的圖表,您具有Status.Status_ID (沒有Status.ID )。此外,您的數據還必須具有相關數據,其中每個表具有相同的值,否則,您將獲得空結果

您的圖表:

在此處輸入圖片說明

更改查詢

SELECT 
    Product.Product_Name,
    Product.Price,
    Product.Image,
    Gender.Description,
    Age.Description,
    Status.Availability
FROM
    Product
JOIN
    Age ON Age.Age_ID
JOIN
    Gender ON Gender.Gender_ID
JOIN
    Status ON Status.ID

SELECT 
    Product.Product_Name,
    Product.Price,
    Product.Image,
    Gender.Description,
    Age.Description,
    `Status`.Availability
FROM
    Product
JOIN
    Age ON Product.Age_ID = Age.Age_ID
JOIN
    Gender ON Product.Gender_ID = Gender.Gender_ID
JOIN
    `Status` ON Product.Status_ID = `Status`.Status_ID

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM