简体   繁体   中英

Display MySQL table attributes via a foreign key relationship using PHP

I have two tables job which contains an attribute called employer_id_job that links it to the employer table. I am able to display the contents of job in a table using PHP by using the following code

<?php
    $query = "SELECT * FROM job";
    $result = mysql_query($query);
    $num = mysql_numrows($result);

    $count = 0;
    while ($count < $num)
    {
        $title = mysql_result ($result, $count, "Title");
        $date_posted = mysql_result ($result, $count, "Date_posted");
        $application_deadline = mysql_result ($result, $count, "Application_deadline");
        $description = mysql_result ($result, $count, "Description");
        $years_of_experience = mysql_result ($result, $count, "Years_of_experience");
        $education_level = mysql_result ($result, $count, "Education_level_required");
        $contract = mysql_result ($result, $count, "Contract_type");
        $company = mysql_result ($result, $count, "Company_name");
?>

        <tr>
        <td><font face = "Arial, Helvetica, sans-serif"><? echo $count + 1; ?></font></td>
        <td><font face = "Arial, Helvetica, sans-serif"><? echo $title; ?></font></td>
        <td><font face = "Arial, Helvetica, sans-serif"><? echo $company; ?></font></td>
        <td><font face = "Arial, Helvetica, sans-serif"><? echo $description; ?></font></td>
        <td><font face = "Arial, Helvetica, sans-serif"><? echo $date_posted; ?></font></td>
        <td><font face = "Arial, Helvetica, sans-serif"><? echo $application_deadline; ?></font></td>
        <td><font face = "Arial, Helvetica, sans-serif"><? echo $education_level; ?></font></td>
        <td><font face = "Arial, Helvetica, sans-serif"><? echo $years_of_experience; ?></font></td>
        <td><font face = "Arial, Helvetica, sans-serif"><? echo $contract; ?></font></td>
<?
        $count ++;
    }
?>

My method here breaks down when trying to assign a value to the $company variable, which is held in the employer table. It was my assumption that simply using the same notation as for the rest of my variables would cause the script to follow the foreign key to the employer table and grab the attributes there, but it hasn't done that.

How should I go about accessing the contents of a table related via a foreign key?

select * from job as j, company as c where j.company == c.company

I'm not sure what then name of your fields are so I made my best guess. This allows you to access both fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM