Regarding Mysql Query

Reply

Join Date: Feb 2008
Posts: 1
Reputation: giteshchawla is an unknown quantity at this point 
Solved Threads: 0
giteshchawla giteshchawla is offline Offline
Newbie Poster

Regarding Mysql Query

 
0
  #1
Feb 19th, 2008
hi all,

i have two tables, table1(id, name, address) and table2(id, phone, email, status). now i want to fetch data from both tables and display in a table. what i want is data must be fetched from table1 and table2 when the status in table2 is 1. if status in table2 is 0 then it do not fetch data from table2 but shows data of table1. can anybody tell me how can i fetch data in this case.

help me please.

Gitesh Chawla
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 514
Reputation: techniner is an unknown quantity at this point 
Solved Threads: 19
techniner techniner is offline Offline
Posting Pro

Re: Regarding Mysql Query

 
0
  #2
Feb 19th, 2008
You probably want to use PHP for this.

  1.  
  2. $q="select * from table2";
  3. $r=mysql_query($q);
  4. while ($row = mysql_fetch_array($r)){
  5. $status=$row['status'];
  6. $id=$row['id'];
  7. $phone=$row['phone'];
  8. $email=$row['email'];
  9.  
  10.  
  11. $q2="select * from table1 where id ='$id'";
  12. $r2=mysql_query($q2);
  13. while ($row2 = mysql_fetch_array($r2)){
  14. $id=$row2['id]';
  15. $name=$row['name'];
  16. $address=$row2['address'];
  17. if ($status == 1){
  18. echo "$name - $address - $phone - $email";
  19. } elseif($status == 0){
  20. echo "$name - $address";
  21. }
  22. }


I think your logic is flawed. We must look in table2 to find out if status = 1 or not.

In your original request you said you did not want to fetch the data. I think you meant that you did not want to "display" the data.

Anyhow the above code should do what you want.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC