Jump to content

Select from 2 tables


mrt003003

Recommended Posts

Hi there I have 2 tables:

 

Fleet

FleetName

PlanetName

Status

 

Planet

PlanetName

PlayerName

 

Im trying to write a select query that will search through the fleet table and display Fleet table details Where the PlanetName from the fleet table is = to the PlanetName of the Planet table. Ive tried joining the tables but i dont think the logic quite works..

 

mysql_select_db($database_swb, $swb);
$query_Fleet = sprintf("SELECT fleet.FleetName, planet.PlanetName FROM fleet, planet WHERE fleet.PlanetName = planet.PlanetName");
$Fleet = mysql_query($query_Fleet, $swb) or die(mysql_error());
$row_Fleet = mysql_fetch_assoc($Fleet);
$totalRows_Fleet = mysql_num_rows($Fleet);

 

I want ONLY the Fleets from the planet i am looking at to be displayed not all of them. I have created another query that displays the planet name of the particular one I am looking at as it is parsed from a hyperlink on a previous page. So is there some way i can use both queries together?

 

"SELECT FROM fleet, WHERE $fleet.PlanetName = $planet.PlanetName"

 

Im a little bit confussed, please help :)

Link to comment
https://forums.phpfreaks.com/topic/234536-select-from-2-tables/
Share on other sites

Heres my first query that determines and displays the planet i am looking at:

$colname_Planet = "-1";

if (isset($_GET['recordID'])) {

  $colname_Planet = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);

}

mysql_select_db($database_swb, $swb);

$query_Planet = sprintf("SELECT * FROM planet WHERE PlanetName = %s", GetSQLValueString($colname_Planet, "text"));

$Planet = mysql_query($query_Planet, $swb) or die(mysql_error());

$row_Planet = mysql_fetch_assoc($Planet);

$totalRows_Planet = mysql_num_rows($Planet);

 

So basically i need to use this query to search the Fleet table and display only the records that have the same PlanetName as this one.

 

Thanks :)

Ive changed it to:

 

$query_Fleet = sprintf("SELECT * FROM fleet WHERE PlanetName =  '$Planet'");

 

Im slightly confused because i thought it would need to select the row:

 

$query_Fleet = sprintf("SELECT * FROM fleet WHERE PlanetName =  '$row_Planet ['Planet']'");

 

The first way doesnt give any error at all, but it also doesnt display results when i echo them...  The second way gives me the error:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\SWB\planet.php on line 48

 

Any ideas? :|

Thanks for the help fugix, i must be well stupid thought coz i just cant get working:

 

mysql_select_db($database_swb, $swb);

$query_Planet = sprintf("SELECT * FROM planet WHERE PlanetName = %s", GetSQLValueString($colname_Planet, "text"));

$Planet = mysql_query($query_Planet, $swb) or die(mysql_error());

$row_Planet = mysql_fetch_assoc($Planet);

$totalRows_Planet = mysql_num_rows($Planet);

 

$plans = $row_Planet['PlanetName'];

 

$colname_Fleet = "-1";

if (isset($_GET['recordID'])) {

  $colname_Fleet = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);

}

mysql_select_db($database_swb, $swb);

$query_Fleet = sprintf("SELECT * FROM fleet WHERE PlanetName =  $plans");

$Fleet = mysql_query($query_Fleet, $swb) or die(mysql_error());

$row_Fleet = mysql_fetch_assoc($Fleet);

$totalRows_Fleet = mysql_num_rows($Fleet);

 

I get: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Calamari' at line 1

 

 

It must be close...

$plans need to be wrapped in (single) quotes within your $query_Fleet string.

 

However  you could do the above in one query using a simple join

SELECT p.PlanetName, p.PlayerName, f.FleetName, f.Status FROM Planets p
LEFT JOIN Fleet f ON (p.PlanetName = f.PlanetName)
WHERE p.PlanetName = '$colname_Planet'

Can i just ask... i assume its possible to add a 3rd table (Ships):

 

$fleetships = $row_Fleet['FleetName'];

mysql_select_db($database_swb, $swb);
$query_Ships = sprintf("SELECT * FROM ships WHERE FleetName = '$fleetships'");
$Ships = mysql_query($query_Ships, $swb) or die(mysql_error());
$row_Ships = mysql_fetch_assoc($Ships);
$totalRows_Ships = mysql_num_rows($Ships);

 

Which leads me to another question if i may, will it work if the outputted results of Fleet and Ships are both in a php do loop? So that the Fleet loop surrounds the Ships loop???

Ive just tried this and the $Ship query isnt outputting anything...

 

I did wonder if it was  because of the order of the code and modified it to:

$colname_Planet = "-1";
if (isset($_GET['recordID'])) {
  $colname_Planet = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);
}
mysql_select_db($database_swb, $swb);
$query_Planet = sprintf("SELECT * FROM planet WHERE PlanetName = %s", GetSQLValueString($colname_Planet, "text"));
$Planet = mysql_query($query_Planet, $swb) or die(mysql_error());
$row_Planet = mysql_fetch_assoc($Planet);
$totalRows_Planet = mysql_num_rows($Planet);

$plans = $row_Planet['PlanetName'];

$colname_Fleet = "-1";
if (isset($_GET['recordID'])) {
  $colname_Fleet = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);
}
mysql_select_db($database_swb, $swb);
$query_Fleet = sprintf("SELECT * FROM fleet WHERE PlanetName =  '$plans'");
$Fleet = mysql_query($query_Fleet, $swb) or die(mysql_error());
$row_Fleet = mysql_fetch_assoc($Fleet);
$totalRows_Fleet = mysql_num_rows($Fleet);

$fleetships = $row_Fleet['FleetName'];

$colname_Ships = "-1";
if (isset($_GET['recordID'])) {
  $colname_Ships = (get_magic_quotes_gpc()) ? $_GET['recordID'] : addslashes($_GET['recordID']);
}
mysql_select_db($database_swb, $swb);
$query_Ships = sprintf("SELECT * FROM ships WHERE FleetName = '$fleetships'");
$Ships = mysql_query($query_Ships, $swb) or die(mysql_error());
$row_Ships = mysql_fetch_assoc($Ships);
$totalRows_Ships = mysql_num_rows($Ships); ?>
<?php do { ?>
<?php echo $row_Fleet['FleetName']; ?>
<?php echo $row_Fleet['PlanetName']; ?>
<?php do { ?>
<?php echo $row_Ships['ShipName']; ?>
<?php } while ($row_Ships = mysql_fetch_assoc($Ships)); ?>
<?php } while ($row_Fleet = mysql_fetch_assoc($Fleet)); ?>

 

What am i doing wrong?

 

Thanks

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.