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
https://forums.phpfreaks.com/topic/93734-arrays/
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
https://forums.phpfreaks.com/topic/93734-arrays/#findComment-480307
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
https://forums.phpfreaks.com/topic/93734-arrays/#findComment-480313
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
https://forums.phpfreaks.com/topic/93734-arrays/#findComment-480317
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
https://forums.phpfreaks.com/topic/93734-arrays/#findComment-480330
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.