简体   繁体   English

MySQL的PHP​​表联接

[英]mysql php table join

I am probably doing something stupid but have been working on this for a day and can't seem to find error. 我可能正在做一些愚蠢的事情,但是已经为此工作了一天,似乎找不到错误。

I have a join of several tables. 我有几个表的联接。 When I print out the results using print_r(mysql_fetch_array($res) , I see a value for the field I want. However, when I try to echo this out or otherwise get access to it, it seems to be empty. Can anyone spot what I am doing wrong? Thanks in advance as this is driving me crazy. 当我使用print_r(mysql_fetch_array($ res)打印出结果时,我看到了我想要的字段的值。但是,当我尝试将其回显或以其他方式访问它时,它似乎是空的。我在做什么错?预先感谢,因为这使我发疯。

tables (some fields omitted) 表(省略一些字段)

members
id|username|password|datetime|email
membercontact
id|memberid|contactid

php
$sql = "SELEcT m.*,mc.*,mc.contactid as mccon
from `members` m
LEFT JOIN `membercontact` mc
on m.id = mc.memberid
WHERE m.id = '147'"

$res = mysql_query($sql);
while($row = mysql_fetch_array($res)) {
echo $row['contactid'];
echo $row['mccon']
}

output: nothing . 输出:没有。 I've also tried other ways to access the contactid field. 我还尝试了其他方法来访问contactid字段。 Other fields do print such as $row['password'] 其他字段也会打印,例如$ row ['password']

However when I use print_r on the results I can see following: 但是,当我在结果上使用print_r时,我可以看到以下内容:

Array ( [0] => v [username] => v [1] => pass [password] => pass [2] => 147 [id] => 147 [3] => 2013-09-05 12:08:03 [datetime] => 2013-09-05 12:08:03 [4] => vz@aol.com [email] => v@aol.com [19] => 933 [contactid] => 933 [20] => 147 [memberid] => 147 [24] => 933 [contactid] => 933 ) 数组([0] => v [用户名] => v [1] =>通过[密码] =>通过[2] => 147 [id] => 147 [3] => 2013-09-05 12: 08:03 [datetime] => 2013-09-05 12:08:03 [4] => vz@aol.com [电子邮件] => v@aol.com [19] => 933 [contactid] => 933 [20] => 147 [memberid] => 147 [24] => 933 [contactid] => 933)

The only thing weird I notice is that some fields are repeated more than once, notably contactid though I cannot say why. 我唯一奇怪的是,有些字段重复了不止一次,尽管我无法说出原因,但值得注意的是, contactid重复了多次。 (The reason for the skips in numbers is there are a lot of other extraneous fields omitted.) (之所以跳过数字,是因为省略了许多其他无关的字段。)

Because you have used LEFT JOIN and if there is one to many relation you probably have to use GROUP BY 因为您使用过LEFT JOIN并且如果存在一对多关系,则可能必须使用GROUP BY

$sql = "SELEcT m.*,mc.*,mc.contactid as mccon
from `members` m
LEFT JOIN `membercontact` mc
on m.id = mc.memberid
WHERE m.id = '147'"
GROUP BY m.id

Try this: 尝试这个:

$sql = "SELEcT m.*,mc.*,mc.contactid as mccon 
from `members` m,
`membercontact` mc
WHERE mc.memberid = m.id AND m.id = '147'"

Its not join issue , its how you have selected the columns from each table. 它不是联接问题,它是如何从每个表中选择列的。 Check following source 检查以下来源

<?php
 $query="select 'hame1' as name, 'name2' as name";
 $con = mysql_connect ('localhost','root','root',true) or die ('connection to db failed');
 mysql_select_db('test_db',$con);

 $res=mysql_query($query);
 if($res){
    while($row=mysql_fetch_array($res)){
      var_dump($row);   
    }   
 }else{
     echo mysql_error($con);
 }  

?>

Will output 将输出

array
  0 => string 'hame1' (length=5)
  'name' => string 'name2' (length=5)
  1 => string 'name2' (length=5)

You would argu why the hell three values if I had only fetch two . 您可能会争辩说,如果我只取两个值,为什么要分配三个值。 Its bcz the name for both column alias are same ie name . 两个列别名的名称相同,即name If you want to fix the issue make sure your column name does not collide and select only desired columns from each table that is efficient and readable also, if column names collide give it an alias eg 如果要解决此问题,请确保您的列名不冲突,并且select only desired columns from each table that is efficient and readable also, if column names collide give it an alias例如

 $query="SELEcT m.column1,m.column2 ,mc.column1 as mc_column1 ...."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM