zizzi0n Posted March 29, 2009 Share Posted March 29, 2009 Hi Guys, Need some help if you would Basically I want to Select the Job ID and find what client was used for that job. Once i have that i want to use that value to query the clients table and select the values related to that client. I put what code i have below. Only thing is, it isnt displaying it <?php $id = 22; $query = $db->execute("select client from `jobs` where `id` = '".$id."' LIMIT 1"); $job = $query->fetchrow; while($job = $query->fetchrow()) { $query2 = $db->execute("select name, address, town, postcode, telephone from `clients` where `name` = ".$job['client']." LIMIT 1"); $client = $query2->fetchrow; while($client = $query2->fetchrow()) { echo "<table width='100%' border='0' cellspacing='10'>"; echo "<tr><td>". $client['name'] ."</td></tr>"; echo "<tr><td>". $client['address'] ."</td></tr>"; echo "<tr><td>". $client['town'] ."</td></tr>"; echo "<tr><td>". $client['postcode'] ."</td></tr>"; echo "<tr><td>". $client['telephone'] ."</td></tr>"; echo "</table><br />"; } } ?> Any help? Link to comment https://forums.phpfreaks.com/topic/151603-solved-using-the-result-from-one-query-to-select-values-from-a-different-table/ Share on other sites More sharing options...
steelaz Posted March 29, 2009 Share Posted March 29, 2009 You can do it using sub-query: <?php $query = $db->execute("SELECT `name`, `address`, `town`, `postcode`, `telephone` FROM `clients` WHERE `name` = (SELECT `client` FROM `jobs` WHERE `id` = 22) LIMIT 1"); $client = $query->fetchrow; // Dump result to see what we've got echo "<pre>"; var_dump($client); echo "</pre>"; while($client = $query->fetchrow()) { echo "<table width='100%' border='0' cellspacing='10'>"; echo "<tr><td>". $client['name'] ."</td></tr>"; echo "<tr><td>". $client['address'] ."</td></tr>"; echo "<tr><td>". $client['town'] ."</td></tr>"; echo "<tr><td>". $client['postcode'] ."</td></tr>"; echo "<tr><td>". $client['telephone'] ."</td></tr>"; echo "</table><br />"; } ?> If this code doesn't work, try running query in phpMyAdmin and see what results you get. Link to comment https://forums.phpfreaks.com/topic/151603-solved-using-the-result-from-one-query-to-select-values-from-a-different-table/#findComment-796232 Share on other sites More sharing options...
zizzi0n Posted March 29, 2009 Author Share Posted March 29, 2009 Awesome, it works perfect Thanks for the quick response. Link to comment https://forums.phpfreaks.com/topic/151603-solved-using-the-result-from-one-query-to-select-values-from-a-different-table/#findComment-796234 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.