Jump to content

Multiple table query and $_GET ??


Bobd3142

Recommended Posts

As of right now I have two MySQL tables.... one with manufacturers and then one with items. They're called "Manuers" and "Items."

 

Items has a field called "manu" with a number corresponding to the ID in "Manu."

 

My goal is to have a file (file.php for example) that will get the items with a given ID (file.php?id=10) and then display only those results. Right now with the following code.. it displays everything, since I can't get anything with $_GET to work at all..

 

<?php
mysql_connect("website","username","password");
mysql_select_db("database");
$result = mysql_query("SELECT Manuers.*, Items.* FROM Manuers, Items 
WHERE Manuers.id = Items.Manu");
while($r=mysql_fetch_array($result)) {
$Name = $r["Name"];
$Picture = $r["Picture"];

echo "$Name - $Picture";
echo "<br />";

}
?>

 

Also, how would I go about making $r only get data from the Items table and not Manuers? It works like that right now but I'm not quite sure why.

 

Thanks for all the help :D

Link to comment
https://forums.phpfreaks.com/topic/163721-multiple-table-query-and-_get/
Share on other sites

So this code makes some assumptions:

+The tables are properly setup (id and manu are of same type)

+name is the first column, and picture is the second column returned in the result set

+I didn't make any errors (i remembered how to use list() = mysql_.. correctly)

<?php

$id = ( isset($_GET['id']) ) ? intval($_GET['id']) : 1;

mysql_connect("website","username","password");
mysql_select_db("database");

$query = "SELECT m.*, i.* 
FROM Manuers m 
JOIN Items i 
  ON(m.id = i.manu)
  WHERE m.id = $id";

$result = mysql_query($query);

while( list($name,$pic) = mysql_fetch_array($result) ) {

   echo "$name - $picture";
   echo "<br />";
   
}
?>

If no id is sent to the page, it will default to 1.  Try it, and if it works, we can refine it from there.

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.