Jump to content

Compare a field in two querys


mrt003003

Recommended Posts

Hi there im not quite sure where im going wrong here.

 

I have 2 queries; the first selects all records in a table == Session username. The second selects all records in a table <> Session username

 

Both work independently but when i try to compare a field in each table (The: PlanetName) it doesnt work. Im trying to make it so that if the same PlanetName is in  the first query and the second in any of the records it will output an ID.

 

<?php $colname_Fleet = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_Fleet = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_Fleet = sprintf("SELECT FleetName, PlanetName FROM fleet WHERE PlayerName = %s", GetSQLValueString($colname_Fleet, "text"));
$Fleet = mysql_query($query_Fleet, $swb) or die(mysql_error());
$totalRows_Fleet = mysql_num_rows($Fleet);

$colname_FleetE = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_FleetE = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_FleetE = sprintf("SELECT FleetName, PlanetName FROM fleet WHERE PlayerName <> %s", GetSQLValueString($colname_FleetE, "text"));
$FleetE = mysql_query($query_FleetE, $swb) or die(mysql_error());
$totalRows_FleetE = mysql_num_rows($FleetE);



while ($row_FleetE = mysql_fetch_assoc($FleetE) && $row_Fleet = mysql_fetch_assoc($Fleet)){

if($row_Fleet['PlanetName'] == $row_FleetE['PlanetName']){
echo $row_Fleet['FleetName'];
echo' ';
}}
?>

 

Any help would be appreciated.

 

Thanks :)

Link to comment
Share on other sites

Ive been playing with the code and found that the first query only need select a single record from its ID:

<?php 
$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 FleetName = %s", GetSQLValueString($colname_Fleet, "text"));
$Fleet = mysql_query($query_Fleet, $swb) or die(mysql_error());
$row_Fleet = mysql_fetch_assoc($Fleet);
$totalRows_Fleet = mysql_num_rows($Fleet);


$colname_PlanetName = $row_Fleet['PlanetName'];

$colname_FleetE = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_FleetE = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_FleetE = sprintf("SELECT FleetName, PlanetName FROM fleet WHERE PlayerName <> %s AND PlanetName=%s", GetSQLValueString($colname_FleetE, "text"),GetSQLValueString($colname_PlanetName, "text"));
$FleetE = mysql_query($query_FleetE, $swb) or die(mysql_error());
$totalRows_FleetE = mysql_num_rows($FleetE);

<?php while ($row_FleetE = mysql_fetch_assoc($FleetE)){

if ($row_FleetE['PlanetName'] == 'Mon Calamari'){
echo $row_Fleet['FleetName'];
echo' ';
}} ?>

 

This way it works! However it outputs the same $row_Fleet['FleetName'] as many times as it checks each of the FleetE records.

 

So my question has changed to: How can i limit the number of outputs to a single one???

Link to comment
Share on other sites

There are other ways to do this, but this will work.

 

<?php 
     $blnMatch = false;
     while(($row_FleetE = mysql_fetch_assoc($FleetE) && (!blnMatch)){

if ($row_FleetE['PlanetName'] == 'Mon Calamari'){
           $blnMatch = true;
   echo $row_Fleet['FleetName'];
   echo' ';
}}
?>

Link to comment
Share on other sites

Hi there thanks for the reply, ive tested it and get a wierd error ive never seen before: Notice: Use of undefined constant blnMatch - assumed 'blnMatch' in C:\wamp\www\SWB\fleet_edit.php on line 269

 

<?php 
$blnMatch = false;
     while(($row_FleetE = mysql_fetch_assoc($FleetE) && (!blnMatch)){

if ($row_FleetE['PlanetName'] == $colname_PlanetName){
           $blnMatch = true;
   echo $row_Fleet['FleetName'];
   echo' ';
}} ?>

 

I had to remove one of the braces on left of th while else it would error: Parse error: syntax error, unexpected '{' in C:\wamp\www\SWB\fleet_edit.php on line 269

I cant see the problem with {.

 

I also wondered if it could be ! infrom of the blnMatch on the right of the while loop. If i removed the ! id get many errors: Notice: Use of undefined constant blnMatch - assumed 'blnMatch' in C:\wamp\www\SWB\fleet_edit.php on line 269

 

That would be outputted as many times as their are records that are being searched in the while loop.

 

Its close i can feel it in my bones. What would you suggest please??

 

Thanks

 

 

Link to comment
Share on other sites

FLEET TABLE

FleetName

PlanetName

Detected

PlayerName

 

The first query Fleet searches the table where the FleetName equal to a url parsed parameter from a previous page.

 

<?php $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 FleetName = %s", GetSQLValueString($colname_Fleet, "text"));
$Fleet = mysql_query($query_Fleet, $swb) or die(mysql_error());
$row_Fleet = mysql_fetch_assoc($Fleet);
$totalRows_Fleet = mysql_num_rows($Fleet); ?>

 

The second query FleetE searches the same table for the all the Fleets where the playername is NOT equal the session username (playername) AND is equal to PlanetName of the previous query Fleet.

 

<?php $colname_PlanetName = $row_Fleet['PlanetName'];

$colname_FleetE = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_FleetE = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_FleetE = sprintf("SELECT FleetName, PlanetName FROM fleet WHERE PlayerName <> %s AND PlanetName=%s", GetSQLValueString($colname_FleetE, "text"),GetSQLValueString($colname_PlanetName, "text"));
$FleetE = mysql_query($query_FleetE, $swb) or die(mysql_error());
$totalRows_FleetE = mysql_num_rows($FleetE);
?>

 

The idea is that all FleetE records are compared with the single Fleet record matching the PlanetName. So if the PlanetName of any FleetE record is the same as the PlanetName in the single Fleet record then i want it to output the single FleetName of the Fleet record. Before i added the $blnMatch variable it would output the same FleetName however many times as there are records in the FleetE query.

 

<?php 
$blnMatch = false;
  while($row_FleetE = mysql_fetch_assoc($FleetE) && (!$blnMatch)){

if ($row_FleetE['PlanetName'] == $colname_PlanetName){
           $blnMatch = true;
   echo $row_Fleet['FleetName'];
   echo' ';
}} ?>

 

I hope ive explained well enough.

 

Thanks

Link to comment
Share on other sites

What am i chatting about... It doesnt work at all. It gives me mental results. Infact when i echo out the 2nd query here it outputs a FleetName that has the same PlayerName as the logged in session username. Which is completely nuts! Furthermore i need to cater for th fact that their maybe more than one FleetE on the same PlanetName as Fleet. Jeees im lost. :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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