Solved

sql query list doesn't display foreign key correctly

Posted on 2009-04-11
4
419 Views
Last Modified: 2013-12-12
Hi Experts

This is my second question - am trying to learn and am having some success but stuck again! Please can anyone help?

I have 2 tables in my database; projects and clients.

projects has 6 columns of data one of which is a foreign key called client_id linked to the  clients table. clients is simply a 2 column table with client_id (primary key) and client_name.

When I query the projects table and list the results I would like the foreign key (client_id) to display the corresponding client_name. I have got this working to a fashion but the client name displayed is always client_id 1 and not the one associated with the project. I'm new to sql and am not sure how to use one query to get info from both tables and list the results as I need them.

At present i'm using 2 queries:

$conn = dbConnect('admin');
$result = mysql_query("SELECT project_id, project_name, client_id, thumbnail, live, project_order FROM projects ORDER BY project_order");
$query_listClients = "SELECT clients.client_name, projects.project_id FROM clients, projects WHERE projects.client_id = clients.client_id";
$listClients = mysql_query($query_listClients, $conn) or die(mysql_error());
$row_listClients = mysql_fetch_assoc($listClients);
$totalRows_listClients = mysql_num_rows($listClients);
$numRows = mysql_num_rows($result);


Please don't laugh at my inexperience!

When listing the results i'm using:

while ($row = mysql_fetch_assoc($result)) {

if($row['live']=="y") {$live="checked=\"checked\"";} else {$live="";}

echo '<li>

<input type="hidden" name="form_order[]" value="'. $row['project_id'] .'" />
<table width="900" border="0" id="projectform">
  <tr>
    <td width="80" align="center">'. $row['project_order'] .'</td>
    <td width="200">'. $row['project_name'] .'</td>
    <td width="200">'. $row_listClients['client_name'].'</td>
    <td width="120" align="center"><input type="checkbox" name="live_check[]" id="live_check"'. $live.'value="' . $row['project_id'] .'" /></td>
    <td width="100" align="center"><img src="../images/thumb/'. $row['thumbnail'] .'" alt="'. $row['project_name'] .'" width="82" height="50" /></td>
    <td width="100" align="center"><a href="project_update.php?project_id='. $row['project_id'] .'">EDIT</a></td>
    <td width="100" align="center"><a href="project_delete.php?project_id='. $row['project_id'] .'">DELETE</a></td>
</tr>
</table>
</li>';

}

?>


I think this is probably straight forward but I can't work it out. I have set the tables to innoDB and indexed the primary key in clients and set up the relationship correctly as far as I can see.

Can anyone help?

thanks
Heather
0
Comment
Question by:heather-allen
  • 2
4 Comments
 
LVL 15

Expert Comment

by:spprivate
ID: 24122158
SELECT
clients.client_name,
projects.project_id FROM PROJECTS
INNER JOIN CLIENTS ON  clients.client_id =projects.client_id

0
 
LVL 7

Accepted Solution

by:
johnnash1180 earned 250 total points
ID: 24122175
I think that you are not iterating the row_listClients list properly.


However, you can do this in single query itself. Use the following query:

SELECT p.project_id, p.project_name, p.client_id, p.client_name, p.thumbnail, p.live, p.project_order FROM projects p, clients c where c.client_id=p.client_id ORDER BY project_order;

Using the above query, you can get the client name in a single query and need not to store the client id client name in a separate list.

Hope it helps.

John Nash



0
 
LVL 1

Author Comment

by:heather-allen
ID: 24122183
thanks spprivate

how will this work with the other query

$result = mysql_query("SELECT project_id, project_name, client_id, thumbnail, live, project_order FROM projects ORDER BY project_order");

does that still need to be separate or can I do it all in one query?
0
 
LVL 1

Author Comment

by:heather-allen
ID: 24122218
thanks john

Tried that - changed p.client_name to c.client_name and it works!

Thank you so much!!!! I have learned something new and very chuffed!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now