简体   繁体   English

使用PHP通过外键关系显示MySQL表属性

[英]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. 我有两个表job ,其中包含一个名为employer_id_job的属性,该属性将其链接到employer表。 I am able to display the contents of job in a table using PHP by using the following code 通过使用以下代码,我可以使用PHP在表中显示job的内容

<?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. 我的方法试图将值分配到这里时打破了$company变量,这是在举行employer表。 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. 我的假设是,简单地使用与其余变量相同的符号,将使脚本遵循employer表的外键并在此处获取属性,但是并没有做到这一点。

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. 这使您可以访问两个字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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