简体   繁体   中英

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.

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. Other fields do print such as $row['password']

However when I use print_r on the results I can see following:

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 )

The only thing weird I notice is that some fields are repeated more than once, notably contactid though I cannot say why. (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

$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 . 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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