php – 使用主表引用子表

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 使用主表引用子表脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的数据库中,我有一个作业表,你可以说它是我数据库中的主表.我有一个名为category的列,此列指向一个名为category的表,该表包含不同的类别.

拿起外键的概念,我把列类别变成了一个外键,它查看了类别表.

php – 使用主表引用子表

在我的类别表中,我确保它指向ID.

php – 使用主表引用子表

当我运行我的网页时,它打印出类别列中的值1,理论上它应该不打印“Driving”?

php – 使用主表引用子表

function getJobDetails($job,$cat){
//this connects to the database
include "connectToDatabse.PHP";

//show me the results From job,where category is like cat vice versa
$results = $pdo->query("SELECT * From job WHERE category LIKE '$cat%' OR tITle LIKE '$job'");
$str = "<table>";                       //PRints out table
$str .= "<td>" ."Title" . "</td>";      //First row
$str .= "<td>" ."Reference" . "</td>";  //N row...
$str .= "<td>" ."Salary(£)" . "</td>";
$str .= "<td>" ."Description" . "</td>";
$str .= "<td>" ."Category" . "</td>";
foreach ($results as $row) {
    $ref = $row['reference'];
    $link = "<form method='get' action='apply.PHP' name='edit'>
         <input tyPE='hidden' name='referenceNumber' value='$ref'>
         <input type='submit' value='$ref'>
    </form>";
    $str .= "<tr>";
    $str .= "<td>" . $row['title'] . "</td>";
    $str .= "<td>" . $row['reference'] . "</td>";
    $str .= "<td>" . $row['salary'] . "</td>";
    $str .= "<td>" . $row['description'] . "</td>";
    $str .= "<td>" . $row['category'] . "</td>";
    $str .= "<td> " .$link . "</td>";
    $str .= "</tr>";
}
$str .= "</table>";
echo $str;
}

上面的代码一个返回作业表中数据的函数.

编辑:引用问题,因为categories列指向类别表,它是否应该将数据引回到作业表?

解决方法

不,不应该.你错过了解外键.它们不会更改您获得的数据,它们只告诉数据库系统’嘿,这引用了其他内容,如果其他条目被更新或删除,请在此处使用此条目执行X(更新,删除,…).您仍然需要联接才能获得预期结果:

SELECT ...,category.title FROM job LEFT JOIN category ON category.id = job.category

脚本宝典总结

以上是脚本宝典为你收集整理的php – 使用主表引用子表全部内容,希望文章能够帮你解决php – 使用主表引用子表所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。