Jump to content

[SOLVED] Using the Result From One Query To Select Values from a Different Table


zizzi0n

Recommended Posts

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?

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.

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.