hawkontvn Posted May 11, 2010 Share Posted May 11, 2010 Hey, im new to coding in php and mysql so need some help here. Making a book library for this project of mine. And right now im trying to figure out how to output a table with book title and author name. I have three tables: *book - bookID, bookTitle, bookISBN, bookYear etc... *author - authorID, authorName *book_has_author - book_bookID, book_bookISBN, author_authorID Book_has_author is the n:m relation table for book --> author. Now, how do I make a table which displays all the books by their title (book.bookTitle) together with the author.authorName? Like: Title | Author | XXX | YYY | ZZZ | WWW | etc... I tried doing some code myself, but as you'll probably figure out im not very good - at all. Code: <?php $con = mysql_connect ("localhost","user","password"); if (!$con) { die('Could not connect to the database: ' . mysql_error()); } $select = mysql_select_db("biblio", $con); if (!$select) { die('Could not select the database: ' . mysql_error()); } $list_books = mysql_query("SELECT * FROM book UNION SELECT * FROM author"); echo "<table border='1'> <tr> <th>Title</th> <th>Author</th> </tr>"; while($row = mysql_fetch_array($list_books)) { echo "<tr>"; echo "<td>" . $row['bookTitle'] . "</td>"; echo "<td>" . $row['authorName'] . "</td>"; echo "</tr>"; } echo "</table>"; ?> Link to comment https://forums.phpfreaks.com/topic/201393-retrieving-and-using-information-from-mysql-database-using-php-help/ Share on other sites More sharing options...
Mchl Posted May 11, 2010 Share Posted May 11, 2010 You need JOINs not UNION SELECT b.bookTitile, a.authorName FROM books AS b INNER JOIN book_has_author AS ba ON b.bookID = ba.book_bookID INNER JOIN author ON ba.author_authorID = a.authorID Link to comment https://forums.phpfreaks.com/topic/201393-retrieving-and-using-information-from-mysql-database-using-php-help/#findComment-1056659 Share on other sites More sharing options...
hawkontvn Posted May 11, 2010 Author Share Posted May 11, 2010 Ok, thanks. Fixed it. But how do I display it through PHP after I fixed the query to include JOIN instead of UNION? Thanks. Link to comment https://forums.phpfreaks.com/topic/201393-retrieving-and-using-information-from-mysql-database-using-php-help/#findComment-1056665 Share on other sites More sharing options...
Mchl Posted May 11, 2010 Share Posted May 11, 2010 What you have now should work fine. Link to comment https://forums.phpfreaks.com/topic/201393-retrieving-and-using-information-from-mysql-database-using-php-help/#findComment-1056685 Share on other sites More sharing options...
hawkontvn Posted May 11, 2010 Author Share Posted May 11, 2010 Well, with my current PHP code the Author column ends up empty. The title column is ok though. Something wrong with the following? Thought 'authorName' should do it. while($row = mysql_fetch_array($list_books)) { echo "<tr>"; echo "<td>" . $row['bookTitle'] . "</td>"; echo "<td>" . $row['authorName'] . "</td>"; echo "</tr>"; } echo "</table>"; Link to comment https://forums.phpfreaks.com/topic/201393-retrieving-and-using-information-from-mysql-database-using-php-help/#findComment-1056696 Share on other sites More sharing options...
hawkontvn Posted May 11, 2010 Author Share Posted May 11, 2010 oh,figured it out... lol. "SELECT bookTitle, bookYear, authorName AS author..." and in the php code further down I used 'authorName', instead of 'author'.. Link to comment https://forums.phpfreaks.com/topic/201393-retrieving-and-using-information-from-mysql-database-using-php-help/#findComment-1056703 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.