Jump to content

Double Query


monkeypaw201

Recommended Posts

So, i have this code again, and i have a previous query that pulls soething from the database and provides an ID relative to another database, and then i need to query again from the database WHERE that ID matches the table. THEN i need to take THAT ID and re-query for a final match.

 

<?php 
$con = mysql_connect("localhost","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("database", $con);
  $result = mysql_query("SELECT * FROM domiciles WHERE id = '$row_Pilot[hub]'");
  while($row = mysql_fetch_array($result))
  {
  $airport = $row['airport'];
  }
  
  $air = mysql_connect("localhost","user","pass");
if (!$air)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("database", $air);
  $result = mysql_query("SELECT * FROM airs WHERE id = '$airport'");
  while($row = mysql_fetch_array($result))
  {
  echo = $row_air['city'];
  }mysql_close($air);
mysql_close($con);
  
		 ?>

Link to comment
Share on other sites

You don't need to connect twice, as long as it is the same database.

 

And, you just need to store your result into different variables and you can have as many queries going as you want:

 

<?php 
$con = mysql_connect("localhost","user","pass")
  or die('Could not connect: ' . mysql_error());
mysql_select_db("database", $con)
  or die('Could not select db: ' . mysql_error());
$result1 = mysql_query("SELECT `airport` FROM `domiciles` WHERE `id` = '{$row_Pilot['hub']}'");
while($domicileRow = mysql_fetch_array($result1)){
  $result2 = mysql_query("SELECT * FROM `airs` WHERE `id` = '{$domicileRow['airport']}'");
  while($airRow = mysql_fetch_array($result2)){
    echo = $airRow['city'];
  }
}
?>

Link to comment
Share on other sites

if it helps when i do this, i works (it echos the right ID)

 

<?php 
$con = mysql_connect("localhost","user","pass");
if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }
 mysql_select_db("database", $con);
 $result = mysql_query("SELECT * FROM domiciles WHERE id = '$row_Pilot[hub]'");
 while($row = mysql_fetch_array($result))
 {
 $airport = $row_con['airport'];
 }
 /**
$air = mysql_connect("localhost","user","pass");
if (!$air)
 {
 die('Could not connect: ' . mysql_error());
 }mysql_select_db("database", $air);
 $result = mysql_query("SELECT * FROM airports WHERE id = '$airport'");
 while($row = mysql_fetch_array($result))
 {
 echo = $row_air['city'];
 }
mysql_close($air);  */
mysql_close($con);
 
		 ?>

 

Turn on error reporting.

 

:/ Never learned how...

Link to comment
Share on other sites

if it helps when i do this, i works (it echos the right ID)

 

<?php 
$con = mysql_connect("localhost","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("database", $con);
  $result = mysql_query("SELECT * FROM domiciles WHERE id = '$row_Pilot[hub]'");
  while($row = mysql_fetch_array($result))
  {
  $airport = $row_con['airport'];
  }
  /**
$air = mysql_connect("localhost","user","pass");
if (!$air)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("database", $air);
  $result = mysql_query("SELECT * FROM airports WHERE id = '$airport'");
  while($row = mysql_fetch_array($result))
  {
  echo = $row_air['city'];
  }
mysql_close($air);  */
mysql_close($con);
  
		 ?>

 

Turn on error reporting.

 

:/ Never learned how...

 

What do you mean it echos the right thing? You aren't echoing anything.

Link to comment
Share on other sites

where is $row_Pilot[hub] coming from??

 

try this

<?php
$con = mysql_connect("localhost","user","pass")
  or die('Could not connect: ' . mysql_error());
mysql_select_db("database", $con)
  or die('Could not select db: ' . mysql_error());
$result1 = "SELECT airs.city AS city FROM domiciles JOIN airs ON domiciles.airport = airs.id WHERE domiciles.id = '{$row_Pilot['hub']}'";
$res =  mysql_query($result1) or die(mysql_error());
  while($airRow = mysql_fetch_array($result1)){
    echo $airRow['city'];
  }
?>

 

Ray

Link to comment
Share on other sites

rhodesa, my mistake, there should be an echo here:

$airport = $row_con['airport']; echo $airport;

i forgot to copy/paste it..

 

---------

 

craygo , the $row_Pilot[hub] is coming from a previous query higher up on the page..

 

---------

 

revraz, just because i'm more comfortable troubleshooting it.. i have had little experience merging them together, and more the more complex ones, i go back to the basics.. :P

 

-------

EDIT:: Would it help if i provide the whole page source?

Link to comment
Share on other sites

FIXED

 

Ok, i kept messing with it and came out with this (Yes, It WORKS)

 

<?php 
$con = mysql_connect("localhost","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("database", $con);
  $result = mysql_query("SELECT * FROM domiciles WHERE id = '$row_Pilot[hub]'");
  while($row_con = mysql_fetch_array($result))
  {
  $airport = $row_con['airport'];
  }
  mysql_close($con);
?>
<?php 
$con = mysql_connect("localhost","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("database", $con);
  $result = mysql_query("SELECT * FROM airports WHERE id = '$airport'");
  while($row_con = mysql_fetch_array($result))
  {
  $city = $row_con['city'];
  echo $city;
  }
  mysql_close($con);
?>

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.