srinivas6203 Posted May 12, 2008 Share Posted May 12, 2008 Hi i am using php and mysql. I want display total records cmpared on accountcode. The accountcode was getting from some table. The records are getting from another table with compare of accountcode. But there are so many accountcodes like 9000,9001,9002 etc. But the records are displayed only with use of single value (ex:9000 or 9001 etc) My code is as follows $sql_gcode=mysql_query("select * from table1 where uid='".$row_uid."' and tech='gtalk'"); while($row_gcode=mysql_fetch_array($sql_gcode)) { $user_gcode=$row_gcode['accountcode']; } $sql_gtalk=mysql_query("select * from table2 where accountcode='".$user_gcode."'"); while($row_gtalk=mysql_fetch_array($sql_gtalk)) { echo '<tr bgcolor="#cddeff"><td>'.$row_gtalk['id'].'</td><td>'.$row_gtalk['accountcode'].'</td><td>'.$row_gtalk['date_created'].'</td> </tr>'; } the accountcode's are 9000,9001,9002 etc are stored in $user_gcode. But the records are displayed only for single accountcode like (where accountcode='9000'). Cn anyone help me out. thanx in advance. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 12, 2008 Share Posted May 12, 2008 The reason that the records are only displayed for one of the codes is that the only thing that occurs in the first loop is the setting of the variable. Each time the loop runs, that variable gets overwritten, so when the next query is made only the last value for the code is used. However, you dont need two queries anyway, you can just use a join. Something like this should work: <?php $sql = "SELECT table1.accountcode,table2.id,table2.date_created FROM table1,table2 WHERE table1.accountcode=table2.accountcode AND table1.uid='$row_uid' AND tech='gtalk'"; $result = mysql_query($sql) or die(mysql_error()); while(list($code,$id,$date) = mysql_fetch_row($result){ echo '<tr><td>'.$id.'</td><td>'.$code.'</td><td>'.$date.'</td></tr>'."\n"; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.