2015-04-07 27 views
0

當我想通過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) 
?> 
+2

表格模式會有幫助。如果沒有看到表格之間的相互關係,你的連接就會被破壞得無法修復。 –

+0

@pala_這裏是表格模式的圖片http://www.ya-techno.com/up/uploads/1428376147411.png – Yousef

+1

可否請加http://sqlfiddle.com,以便我們瞭解您的數據 –

回答

0

你的最後一個加入on Status.ID是問題。 Status表沒有ID列。根據你圖上,你有Status.Status_ID(你沒有Status.ID)此外,您的數據必須具有相關的數據,其中每個表中有共同的價值觀,否則,你會得到空的結果

您的圖表:

enter image description here

更改查詢

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 
+0

評論是不適合擴展討論;這個談話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/74645/discussion-on-answer-by-lea-tano-join-more-than-tables-in-json)。 – Taryn