2013-05-01 76 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并考虑使用左加入。

既然您没有发布您的表的架构,我不能告诉你更多。