monkeypaw201 Posted February 19, 2008 Share Posted February 19, 2008 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 https://forums.phpfreaks.com/topic/91949-double-query/ Share on other sites More sharing options...
revraz Posted February 19, 2008 Share Posted February 19, 2008 Sounds like you should just use the MySQL JOIN if the table all relate. Link to comment https://forums.phpfreaks.com/topic/91949-double-query/#findComment-470877 Share on other sites More sharing options...
monkeypaw201 Posted February 19, 2008 Author Share Posted February 19, 2008 Sounds like you should just use the MySQL JOIN if the table all relate. 1. Don't know that function or what it does 2. I need them all seperate for functunality on the rest of the site Link to comment https://forums.phpfreaks.com/topic/91949-double-query/#findComment-470881 Share on other sites More sharing options...
rhodesa Posted February 19, 2008 Share Posted February 19, 2008 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 https://forums.phpfreaks.com/topic/91949-double-query/#findComment-470885 Share on other sites More sharing options...
craygo Posted February 19, 2008 Share Posted February 19, 2008 $sql = "SELECT * FROM domiciles JOIN airs ON domiciles.airport = airs.id WHERE domiciles.id = '$pilothub'"; You should have $pilothub = some variable Ray Link to comment https://forums.phpfreaks.com/topic/91949-double-query/#findComment-470886 Share on other sites More sharing options...
monkeypaw201 Posted February 19, 2008 Author Share Posted February 19, 2008 Thanks rhodesa; Unfontunately it didnt help i still get a blank page... Link to comment https://forums.phpfreaks.com/topic/91949-double-query/#findComment-470889 Share on other sites More sharing options...
revraz Posted February 19, 2008 Share Posted February 19, 2008 Turn on error reporting. Link to comment https://forums.phpfreaks.com/topic/91949-double-query/#findComment-470898 Share on other sites More sharing options...
monkeypaw201 Posted February 19, 2008 Author Share Posted February 19, 2008 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 https://forums.phpfreaks.com/topic/91949-double-query/#findComment-470904 Share on other sites More sharing options...
rhodesa Posted February 19, 2008 Share Posted February 19, 2008 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 https://forums.phpfreaks.com/topic/91949-double-query/#findComment-470911 Share on other sites More sharing options...
craygo Posted February 19, 2008 Share Posted February 19, 2008 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 https://forums.phpfreaks.com/topic/91949-double-query/#findComment-470914 Share on other sites More sharing options...
revraz Posted February 19, 2008 Share Posted February 19, 2008 Are those tables on the same database? If so, why open two connections anyways? Link to comment https://forums.phpfreaks.com/topic/91949-double-query/#findComment-470915 Share on other sites More sharing options...
monkeypaw201 Posted February 19, 2008 Author Share Posted February 19, 2008 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.. ------- EDIT:: Would it help if i provide the whole page source? Link to comment https://forums.phpfreaks.com/topic/91949-double-query/#findComment-470920 Share on other sites More sharing options...
monkeypaw201 Posted February 19, 2008 Author Share Posted February 19, 2008 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 https://forums.phpfreaks.com/topic/91949-double-query/#findComment-470930 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.