First PHP/MySQL Project Day 6: Joining Tables Together

For the last video in this series, I learned how to join two tables together with cross joins, inner joins, and outer joins. Exciting stuff here! (And I don’t mean that sarcastically. Learning about joins opens up a whole new world of database possibilities!) Anyway, this video is five and a half minutes long. Enjoy!

Links for Joining Tables Together

These sites are chock-full of information. If you just want to review the basics, the first link is all you need:

  • SQL Join – From W3Schools, some examples and explanations of inner joins and outer (left and right) joins.
  • Join Syntax – Everything you ever wanted to know about MySQL’s join syntax, directly from the MySQL manual.
  • Join (SQL) – From Wikipedia, lots of details on many types of joins.

So now that I’ve learned how use joins, I think it’s safe to say I’ve covered the basics of MySQL! I’m hoping this is the first of many milestones. And who knows? Maybe once I learn more advanced stuff, I’ll come back to this project and turn it into something useful.

8 thoughts on “First PHP/MySQL Project Day 6: Joining Tables Together

  1. Cool! :)

    You should take a look at PHP Object Oriented Programming. It is better then doing it the other way because you don’t have to write as much code, well for big projects anyway.

    I’ve been messing about with css, here take a look. Not anything that I would use for a website, except maybe the dark layout, 4.

    http://yaseen.iifree.net/1/index.html
    http://yaseen.iifree.net/2/index.html
    http://yaseen.iifree.net/3/index.html
    http://yaseen.iifree.net/4/index.html

    Still making some more. :)

  2. Hei Liz.

    BRILLIANT!
    Have been trying to crack the codes on these things myself for a good while, so the links are very useful to me indeed.
    Continued Grand Day to you and yours.

  3. Thanks, guys!

    Yas – Nice job on those layouts! I should follow your example and practice my CSS too, lol. And of course the Object Oriented Programming… One more thing on my list!

  4. Excellent tutorial.

    I found this while searching for an example of a php/mysql database as I want to write a simple (very very simple!) help desk application ( I help out at some schools and want the teachers to have a simple way of recording requests/faults).

    Would it be possible, please, to have a copy of the code used in the final tutorial to use as a starting point (to save some typing :) )
    regards
    Simon

  5. Hi, Simon, and thanks! I lost my copy of the the code I used in this series but I typed up what was shown in this last video. It’s just part of what’s required to make everything work, though… this doesn’t include connecting to MySQL, or the rest of the form(s) shown. But hopefully this excerpt is better than nothing.

    (By the way, check out a forum like PHP Freaks for help.)


    // Selecting the data

    $resultselect = mysqlquery('SELECT id, item, todo.tag, deadline, color FROM todo LEFT JOIN tags ON todo.tag = tags.tag');

    // Fetching and displaying the data
    while($row = mysql_fetch_array($resultselect)) { ?>

    <?php if($row['color']) { ?>
    <span stlye="color:<?php echo $row['color'] ?>">
    <?php } ?>

    <input type="checkbox" name="done[]"
      id="<?php echo $row['id'] ?>"
      value="<?php echo $row['id'] ?>" />

    <label for="<?php echo $row['id'] ?>">

    <?php echo $row['item'];

    if($row['tag']) {
      echo ' - tagged ' . $row['tag']; }

    if($row['deadline'] != 0000-00-00) {
      echo ' - due on ' . $row['deadline']; }

    ?>

    </label>

    <?php if($row['color']) { ?>
      </span>

    <?php }

    } ?>

  6. Hi Team,

    I am using the same syntax for my database but its showing me the ids(int) of another table instead of strings(varchar).
    I am using MyISAM engine.
    Does the engine affect joining two tables?

    Here is my code:

    $sql = “SELECT * FROM (org LEFT JOIN category ON org.org_category = category.category_id) ORDER BY org.org_id DESC”;

    What was going wrong?
    Thanks in advance!

  7. Amol,

    If you do a join on two tables that have a column name that is the same, the result set will show only that last one.

    A solution for this is to alias your columns in your SELECT like:

    SELECT o.id as oid, c.id as catid FROM org o LEFT JOIN category on…

    The ‘o.id as oid’ is an example of an alias. Once you alias a column you can get to it in your results by referencing the alias.

    Hope that helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>