2013-05-01 74 views
0

我試圖從多個表中的多個列中獲取值。代碼如下所示:數據庫賦值兩次

<?php 

$connect = mysql_connect("localhost","en","]9"); 

mysql_select_db("en"); 
$result = mysql_query("SELECT title, field_id_1 FROM exp_channel_titles, exp_channel_data") or die(mysql_error()); 

// check for empty result 
if (mysql_num_rows($result) > 0) { 
    // looping through all results 
    // products node 
    $response["video_path"] = array(); 

    while ($row = mysql_fetch_array($result)) { 
     // temp user array 
     $s1 = explode('"',$row['field_id_1']); 
     $path = array(); 
     $path["field_id_1"] = $s1[5]; 
     $path["title"] = $row["title"]; 
     array_push($response["video_path"], $path); 
    } 
    // success 
    $response["success"] = 1; 
    // echoing JSON response 
    echo json_encode($response); 
} else { 
    // no products found 
    $response["success"] = 0; 
    $response["message"] = "No products found"; 

    // echo no users JSON 
    echo json_encode($response); 
} 
?> 

在結果中,值顯示兩次而不是一次。我檢查了兩個表,但這兩個表確實包含同名的列。

+1

現在更改密碼。 – Strawberry 2013-05-01 11:08:36

+0

你需要訪問服務器嗎?發佈前我更改了密碼和數據庫名稱。 – user2064667 2013-05-01 11:15:31

+0

好 - 只需檢查;-) – Strawberry 2013-05-01 11:35:22

回答

0
查詢

Cehck輸出

SELECT標題,field_id_1 FROM exp_channel_titles,exp_channel_data

連接兩個表基礎上的關係,用不同的,這將解決你的問題

+0

將您的查詢作爲代碼 – 2013-05-01 11:16:34

+0

您可以輸入查詢。不知道那樣的複雜查詢。\ – user2064667 2013-05-01 11:18:36

+0

發佈您的表格結構 – 2013-05-01 11:19:32

1

你從你的代碼運行查詢在MySQL客戶端?

你正在進行隱式交叉連接,這就是爲什麼你得到似乎是重複的結果。事實上,你正在獲得兩張表的笛卡爾積。

閱讀更多關於http://en.wikipedia.org/wiki/Join_%28SQL%29#Cross_join並考慮使用左加入。

既然您沒有發佈您的表的架構,我不能告訴你更多。