User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 25 Jul 2003, 12:12   #1
Intervention
In fond memory of SB
 
Join Date: Dec 2002
Location: LIGHTNING BOLT!!
Posts: 393
Intervention is an unknown quantity at this point
PHP/MySQL JOIN difficulties.

I seem to be having great difficulty in getting the desired result a database query. I have two tables (ist_fugitives and ist_caught) populated with two rows of data. These are using the felonID field as their Key thingy. Information about each fugitive is stored in the ist_fugitives table, and once they are apprehended they also get a listing in the ist_caught table. The full structure and two example rows of data:

Code:
CREATE TABLE `ist_caught` (
  `felonID` int(5) NOT NULL default '0',
  `wantedBy` varchar(15) NOT NULL default '',
  `caughtBy` varchar(15) NOT NULL default '',
  `location` varchar(30) NOT NULL default '',
  `date` varchar(12) NOT NULL default '',
  PRIMARY KEY  (`felonID`)
) TYPE=MyISAM;

INSERT INTO `ist_caught` VALUES (1, 'USA001', 'UK001', 'Germany', '23/07/2003');
INSERT INTO `ist_caught` VALUES (12, 'UK001', 'UK001', 'France', '25/07/2003');
INSERT INTO `ist_caught` VALUES (11, 'UK001', 'USA001', 'United States', '25/07/2003');
INSERT INTO `ist_caught` VALUES (15, 'USA001', 'USA001', 'Afghanistan', '25/07/2003');
INSERT INTO `ist_caught` VALUES (16, 'USA001', 'USA001', 'Yemen', '25/07/2003');

CREATE TABLE `ist_fugitives` (
  `felonID` int(5) NOT NULL auto_increment,
  `felonName` varchar(40) NOT NULL default '',
  `felonDOB` varchar(10) NOT NULL default '',
  `felonSex` char(1) NOT NULL default '',
  `felonNationality` varchar(22) NOT NULL default '',
  `felonLocation` varchar(22) NOT NULL default '',
  `felonDescription` varchar(255) NOT NULL default '',
  `felonWantedfor` varchar(255) NOT NULL default '',
  `felonHunters` varchar(15) NOT NULL default '',
  `felonImage` varchar(40) NOT NULL default '',
  `felonStatus` varchar(12) NOT NULL default '',
  PRIMARY KEY  (`felonID`),
  KEY `felonHunters` (`felonHunters`)
) TYPE=MyISAM AUTO_INCREMENT=17 ;

INSERT INTO `ist_fugitives` VALUES (15, 'Usama Bin Laden', '1957', 'M', 'Saudi Arabian', 'Afghanistan', 'Left-handed and walks with a cane. About 6\'4". Brown eyes and hair.', 'Usama Bin Laden is wanted in connection with the August 7, 1998, bombings of the United States Embassies in Dar es Salaam, Tanzania, and Nairobi, Kenya. These attacks killed over 200 people. In addition, Bin Laden is a suspect in other terrorist attacks t', 'USA001', 'laden.jpg', 'caught');
INSERT INTO `ist_fugitives` VALUES (16, 'Abdelkarin Al-Nasser', 'unknown', 'M', 'Saudi Arabian', 'Yemen', 'Brown eyes, black hair. 5\'8", about 170 pounds.', 'Abdelkarim Hussein Mohamed Al-Nasser has been indicted in the Eastern District of Virginia for the June 25, 1996, bombing of the Khobar Towers military housing complex in Dhahran, Kingdom of Saudi Arabia.', 'USA001', 'nasser.jpg', 'caught');
I have a page which shows all fugitives which have been apprehended and the details regarding their apprehension (as extracted from ist_caught). Personal information regarding the fugitive, such as their name and what they are wanted for, are taken from the ist_fugitives table.

The SQL I have constructed to do this (amongst various others I've tried) is:
Code:
SELECT ist_caught.felonID, ist_caught.wantedBy, ist_caught.caughtBy, ist_caught.date, ist_fugitives.felonName, ist_fugitives.felonImage FROM `ist_caught` INNER JOIN `ist_fugitives` ON ist_caught.felonID=ist_fugitives.felonID ORDER BY ist_caught.felonID DESC
The code I'm using to display this information is:

Code:
$DB->Query("SELECT ist_caught.felonID, ist_caught.wantedBy, ist_caught.caughtBy, ist_caught.date, ist_fugitives.felonName, ist_fugitives.felonImage FROM `ist_caught` INNER JOIN `ist_fugitives` ON ist_caught.felonID=ist_fugitives.felonID ORDER BY ist_caught.felonID DESC LIMIT 10");
if($DB->GetNumRows()!=0)
	{
	$rows = $DB->GetNumRows();

	echo "<tr><td colspan=\"5\">$rows</td></tr>";

		while($felonArray=$DB->GetArray())
			{
			$ID = &$felonArray[0];
			$name = &$felonArray[4];
			$wantedby = GetFullName($felonArray[1]);
			$caughtby = GetFullName($felonArray[2]);
			$image = &$felonArray[5];
			$date = $felonArray[3];

			echo "
			<tr>
				<td>
					<font size=\"-1\">
						<a href=\"./controlcentre.php?Session=$Session&ptl=viewfelon&felonID=$ID\">
							$name
						</a>
					</font>
				</td>
				<td>
					<font size=\"-1\">
						$wantedby
					</font>
				</td>
				<td>
					<font size=\"-1\">
						$caughtby
					</font>
				</td>
				<td>
					<font size=\"-1\">
						$date
					</font>

				</td>
				<td>
					<font size=\"-1\">
						<a href=\"$PREFIX/modules/viewphoto.mod.php?felonID='$ID'\" onclick=\"NewWindow(this.href,'name','300','300','yes');return false;\">Photograph</a>
					</font>
				</td>

			</tr>
			";

			}
		}
The data shown is correct, although only one row is there (should be two). An output of the number of rows also shows that there should be two.

This is the same code I'm using to display data from other database queries and I've had no other problem. The only significant difference is that this query contains a join.

I've looked through the MySQL documentation for the JOIN syntax, and after much trial and error this is as close to getting what I want as I can.

Does the problem lie in the SQL or the PHP? Any help appreciated
__________________
Peace, Love and Linux

--------------
My Artwork
Intervention is offline   Reply With Quote
Unread 25 Jul 2003, 15:43   #2
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
I tried it here (replacing $DB:: with mysql_ calls and deleting the "GetFullName") and it gave two rows. Maybe the problem is with your DB layer?

Also, this page is really wide.
__________________
#linux
queball is offline   Reply With Quote
Unread 26 Jul 2003, 09:15   #3
Intervention
In fond memory of SB
 
Join Date: Dec 2002
Location: LIGHTNING BOLT!!
Posts: 393
Intervention is an unknown quantity at this point
Sorry about the page

I'll have a look at this pear DB thing I keep hearing about. Perhaps that will aide me.

Thanks for trying it
__________________
Peace, Love and Linux

--------------
My Artwork
Intervention is offline   Reply With Quote
Unread 26 Jul 2003, 09:44   #4
Mit
Let battle commence
 
Mit's Avatar
 
Join Date: Feb 2002
Location: England
Posts: 732
Mit is a jewel in the roughMit is a jewel in the roughMit is a jewel in the rough
as far as i understand what ur trying to do. u've got table a and table b. Table a has one record per 'key' and table b has many records linked to A record in table a. Yes?

And then, ur wanting to select a row from table a and get ALL associated b elements in one query?

2 ways to do it. Break it down into 2 queries.

Get from a, then select from b where felonID = ...

or

turn the join around, so u select the caugh records with the fugative info merged onto them.

There are probably other, and better ways of doing this, but this is what just came to mind.
__________________
Mit
http://tim.igoe.me.uk - Development Blog
Whats on TV now - UK TV Guide

<Mendosa> mit is a cute cudlly toy that will be in the shops by christmas
<mig-work> ur now my eternal fav pa god
<Squiz> i name thee, Sir Mit
<Zeus> u my friend are a true gamer I knew u were
Mit is offline   Reply With Quote
Unread 26 Jul 2003, 16:20   #5
Raging.Retard
Street Tramp
 
Raging.Retard's Avatar
 
Join Date: Apr 2000
Location: Street Gutter
Posts: 341
Raging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant future
Quote:
Originally posted by Mit
as far as i understand what ur trying to do. u've got table a and table b. Table a has one record per 'key' and table b has many records linked to A record in table a. Yes?
From the way I read it, its one-to-one (assuming a felon can only be caught once). The SQL here looks fine, as for your php.. well I dont 'speak it' per say.
__________________
Chimney Pots.
Raging.Retard is offline   Reply With Quote
Unread 27 Jul 2003, 19:06   #6
Intervention
In fond memory of SB
 
Join Date: Dec 2002
Location: LIGHTNING BOLT!!
Posts: 393
Intervention is an unknown quantity at this point
It was a one-to-one sort of thing (felon can only be caught once, although they *have* to be caught).

Queball was right in that it was the DB Abstraction Layer. I've changed to PEAR DB and it works fine now.



Also, for anybody interested I've just released the first version of this over on SourceForge .
__________________
Peace, Love and Linux

--------------
My Artwork
Intervention is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 14:44.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018