MySQL query pulling from two tables, display in correct fields

Posted by Matt Nathanson on Stack Overflow See other posts from Stack Overflow or by Matt Nathanson
Published on 2010-04-27T21:51:52Z Indexed on 2010/04/27 21:53 UTC
Read the original article Hit count: 270

Filed under:
|
|
|
|

I'm trying to select all fields in two separate tables as long as they're sharing a common ID.

//mysql query
$result = mysql_query("SELECT * FROM project, links 
WHERE project.id = links.id and project.id = $clientID")

//displaying the link 
if ($row['url'] != null){
    echo "<div class='clientsection' id='links'>Links</div>";
    echo "<a class='clientlink' id='link1' href='" . $row['url'] . "'>" . $row['name'] . "</a>";
}
else {
echo "<a class='clientlink' id='link1' href='" . $row['url'] . "' style='display:none;'>" . $row['name'] . "</a>";
        };

As you can see, my tables are "projects", and "links" Each is sharing a common field "id" for reference. It looks as though where both links.id and project.id are equal, it outputs anything, but when there is no links.id associated with a given $clientID the container relative to the $clientID doesn't display at all.

Essentially I'm using this to add links dynamically to a specific client in this CMS and if there are no links, I want the container to show up anyway.

Hopefully I've expressed myself clearly, any pointers in the right direction are appreciated. Thanks!

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php