Jump to content

Inner join 2 tables?


phpstuck

Recommended Posts

The code below returns results when a person loans a book to someone. It provides an array result that shows which person has which books currently signed out.What I am trying to do is bring info from another table into the results.

 

The primary table this works off of returns the name of the borrower and the UPC number of the book they have borrowed as well as the date it was checked out. I have another table named BOOK where the same UPC code is located but it has the title of the book associated with it.

 

In the table named LOANBOOK the UPC is called "loanupc"

In the table named BOOK the UPC is called just "upc"

 

Can I run a query with INNER JOIN that would put the title of the book in the returned results based on matching those UPC codes.

 

Here is the code for what I have which returns only info from the LOANBOOK table.

 

<?PHP

include_once 'db.php';
include_once 'header.html';
include_once 'reportheader.html';

$sql = mysql_query(
"ALTER TABLE loanbook ORDER BY name ASC");
echo "<hr>";
$deflist=mysql_query(
        "SELECT loanupc, name, dateout, datein FROM loanbook WHERE datein IS NULL");
while ($all = mysql_fetch_array($deflist)) {
   $results[$all['name']][] = array ('loanupc' => $all['loanupc'], 'dateout' => $all['dateout'] );
}

//print_r($results);
error_reporting(0);
echo '<center><TABLE id=AutoNumber22 style="BORDER-COLLAPSE: collapse" borderColor=#000000 
      height=12 cellSpacing=3 cellPadding=3 width=600 border=1>
      <TBODY>
      <TR><TD>Books Currently loaned out</TR></TD><TR><TD>';

foreach ($results as $catName => $catData)
{
   print('<center><TABLE id=AutoNumber20 style="BORDER-COLLAPSE: collapse" borderColor=#000000 
      bgcolor=green height=12 cellSpacing=3 cellPadding=3 width=600 border=1>
      <TBODY>
      <TR><TD>
	<b><font face=arial size=2 color=white>'.$catName.'</b><br/></font></td></tr></table></center>'."\n");
   foreach ($catData as $itemNum => $itemData)
   
   {
error_reporting(1);
      // if you want to access the row data in this loop, use the following method:
      print('<center><TABLE id=AutoNumber21 style="BORDER-COLLAPSE: collapse" borderColor=#000000 
      height=12 cellSpacing=3 cellPadding=3 width=600 border=0>
      <TBODY>
      <TR><TD><font face=arial size=2>
<A HREF="editproduct.php?upc='.$itemData['loanupc'].'">edit</A> - ' .
      $itemData['loanupc'].', '.$itemData['dateout'].'<br/></td></tr></table></center></font>'."\n"); 
      // etc. (you must code the field names in hard coded this way)
      //foreach ($itemData as $fieldName => $value)
      
   }
}
echo '</td></tr></table></center>';

include_once 'footer.html';

?>

Link to comment
https://forums.phpfreaks.com/topic/193192-inner-join-2-tables/
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.