chauhanRohit Posted May 2, 2014 Share Posted May 2, 2014 This is the code $query = mysql_query("SELECT fname,lname FROM structure.authors "); $query1 = mysql_query("SELECT book_name FROM structure.books"); $count = mysql_num_rows($query); $count1 = mysql_num_rows($query1); if($count ==0 and $count1 ==0 ){ $output = '<div>no results</div>'; }else{ $output = '<div>'; while($row = mysql_fetch_array($query)){ $output .='<div> '.$row['fname'].' '.$row['lname'].' </div>'; } $output .='<div>'; //the output from second table $output1 = '<div>'; while($row = mysql_fetch_array($query1)){ $output1 .='<div> '.$row['book_name'].' </div>'; } $output1 .='<div>'; } echo $output; echo $output1; Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 2, 2014 Share Posted May 2, 2014 Is there a connection between the two database tables? For example, are the author IDs referenced in the books table? If so, you could look into merging the queries with a LEFT JOIN: http://www.tizag.com/mysqlTutorial/mysqlleftjoin.php Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 2, 2014 Share Posted May 2, 2014 WTF? He asked the exact same question on Monday on Devshed, got some pretty extensive answers and seemed to be satisfied. Now he's starting all over again? I thought we cleared that up, chauhanRohit? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 2, 2014 Share Posted May 2, 2014 programming requires that you define what data you have as inputs to your code and what output you want to produce from that data. all you have stated is you want to combine two outputs. you haven't shown or stated what output you want or what is wrong with the output your code currently produces. if you post some example data from your two tables, 2 or 3 rows each would be enough, and show what exact output you want to produce for that example data, someone can probably help you. Quote Link to comment Share on other sites More sharing options...
chauhanRohit Posted May 3, 2014 Author Share Posted May 3, 2014 (edited) This is the output that i get when i a use this code, I have 2 tables authors and books and i want to display the author names and book names from the two tables $query = mysql_query("SELECT authors.fname,authors.lname,books.book_name from authors,books ") or die ('cannot connect'); $count = mysql_num_rows($query); if($count ==0){ $output = 'no results'; }else{ $output = '<div>'; while($row = mysql_fetch_array($query)){ $output .='<div> '.$row['fname'].' '.$row['lname'].'</br> '.$row['book_name'].' </br></br> </div>'; } $output .='<div>'; } echo $output; THE output sydney sheldonIf Tomorrow Comes Jeffrey archerIf Tomorrow Comes Ruskin Bond Chetan BhagatIf Tomorrow Comes sydney sheldonMaster of the Game Jeffrey archerMaster of the Game Ruskin BondMaster of the Game Chetan Bhagat The output that i want is sydney sheldon Jeffrey archer Ruskin Bond Chetan Bhagat If Tomorrow Comes Master of the Game Edited May 3, 2014 by chauhanRohit Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 3, 2014 Share Posted May 3, 2014 you have now switched your code to use a query that's join'ing every row from the author table with every row from the book table. at this point, i don't think we can help you. all you are doing in this thread and in the thread in the other help site is to randomly change things without any reason behind it. you were also told in the other help site that if there's no relationship between the two pieces of output, that you need to use two queries. Quote Link to comment Share on other sites More sharing options...
chauhanRohit Posted May 4, 2014 Author Share Posted May 4, 2014 (edited) hey the first code i posted is giving me the result that i want but as i said earlier it has two outputs, so i tried editing the code and posted the new code here, i dont know why this annoyed you guys i am just looking for a solution here. if you see i have managed to get the results as one outputs in my second code , but the results are being duplicated can you just tell me a way to avoid this. as per the relationship between the two tables i have added a foreign key in the books table (author_id), all i want is the names and the book names from the two tables. if this not possible , can u suggest a way to achieve this. Edited May 4, 2014 by chauhanRohit Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 4, 2014 Share Posted May 4, 2014 The very last attempt: First of all, you need two queries. Not one. Two. One query for the authors, one for the books. Just like in your first post. OK? Now comes the “magic”: Instead of putting the authors into $output and the books into $output1 (which logically leads to “two outputs”), you put them all into $output. So you have one variable. Not two. One. OK? So the result sets of two queries go into one variable, just like you wanted. Not sure why this is so hard to understand. Quote Link to comment Share on other sites More sharing options...
Solution chauhanRohit Posted May 5, 2014 Author Solution Share Posted May 5, 2014 (edited) Now comes the “magic”: Instead of putting the authors into $output and the books into $output1 (which logically leads to “two outputs”), you put them all into $output. So you have one variable. Not two. One. OK? So the result sets of two queries go into one variable, just like you wanted. Not sure why this is so hard to understand. Thanks man this helped . finally Edited May 5, 2014 by chauhanRohit 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.