Jump to content

arrays


m00nz00mer

Recommended Posts

hey there, i have an array that selects all booktitles and authors in a database however some books have more than one author... im trying to display them using this code...

 

$result = mysql_query("SELECT bookISBN,bookYear, bookTitle FROM nbc_book");

while($row = mysql_fetch_array($result)) {

 

$authArray = mysql_fetch_array($authNameResults);

 

//Display the results with links to each category

echo '<div class="categoryLinks"><div class="detailTextGrey">Book Title: </div><a href="bookinfo.php?bookID='.$row['bookISBN'].'">'.$row['bookTitle'].'</a><br /><div class="detailTextGrey">Author(s): </div><div class="detailText"><div class="detailText">';

while($row2 = mysql_fetch_array($authArray)) {

echo($row2['authorName']);

}

echo ' ('.$row['bookYear'].') </div></div><br/><br/>';

}

 

?>

 

however im getting this error...

 

mysql_fetch_array(): supplied argument is not a valid MySQL result resource

 

does anyone know whats wrong with this...? Ty

Link to comment
Share on other sites

where are you getting $authNameResults from?

 

it is always a good idea when developing to use an "or die()" statement on every mysql_query() call.

 

 

like so:

 

$query = "SELECT bookISBN,bookYear, bookTitle FROM nbc_book";
$result = mysql_query($query) or die("Query: ".$query."<br>".mysql_error());

 

cheers,

Link to comment
Share on other sites

sorry this is the full code...

 

<?php

 

$authName =

 

"SELECT nbc_author.authorName

FROM nbc_author

INNER JOIN nbc_authbook

ON nbc_author.authorID = nbc_authbook.authorID";

 

$authNameResults = mysql_query($authName);

 

//Retriving all of the books

$result = mysql_query("SELECT bookISBN,bookYear, bookTitle FROM nbc_book");

while($row = mysql_fetch_array($result)) {

 

$authArray = mysql_fetch_array($authNameResults);

 

//Display the results with links to each category

echo '<div class="categoryLinks"><div class="detailTextGrey">Book Title: </div><a href="bookinfo.php?bookID='.$row['bookISBN'].'">'.$row['bookTitle'].'</a><br /><div class="detailTextGrey">Author(s): </div><div class="detailText">'.$authArray['authorName'].' ('.$row['bookYear'].') </div></div><br/><br/>';

}

 

?>

 

Link to comment
Share on other sites

please note what i said and tell us if php says anything...

 

also try this:

 

$result = mysql_query("SELECT bookISBN,bookYear, bookTitle FROM nbc_book");
         while($row = mysql_fetch_array($result)) {
         
         //Display the results with links to each category
         echo '<div class="categoryLinks"><div class="detailTextGrey">Book Title: </div><a href="bookinfo.php?bookID='.$row['bookISBN'].'">'.$row['bookTitle'].'[/url]
<div class="detailTextGrey">Author(s): </div><div class="detailText"><div class="detailText">';
while($row2 = mysql_fetch_array($authNameResults)) {
      echo($row2['authorName']);
   }
      echo ' ('.$row['bookYear'].') </div></div>

';
   }

Link to comment
Share on other sites

ok sry im in a rush, basiclly you need to first query the books, then inside the loop, query the authors for that book.

 

i would give code example but in a rush sry.

 

sorry my sql is really bad but an easier way would be to store all the books of a specific author in an author table, then store books in a seperate table with an author field.

then you would "Select * From `author_table` WHERE `authorname`='$book_author';" inside your book loop

 

hope this helps and good luck

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.