外键输出PHP

时间:2015-03-23 10:42:47

标签: php foreign-keys

我想要抓住外键,因为我需要将它用于画廊,每个上传的图像都分配给摄影师,这些摄影师也需要在菜单中显示。

我跟着this guide,一切顺利。我现在需要用PHP输出数据 - 这是我无法弄清楚的。

<?php

$sql = "SELECT * FROM borrowed WHERE employee.id = 'Reck' JOIN employee ON employee.id = borrowed.employeeid";
$result = mysqli_query($db, $sql);

while($row = mysqli_fetch_array($result)) {
?>

<? echo $row['lastname']; ?>

<?php

   }

?>    

我收到错误警告:mysqli_fetch_array()要求参数1为mysqli_result,第12行/Applications/MAMP/htdocs/galleri/test.php中给出布尔值

第12行是while循环。

1 个答案:

答案 0 :(得分:0)

您的查询中的错误请参阅查询结构How to use joins

在您的查询中,joinwhere条件之后,但必须在where之前。它应该是这样的

$sql = "SELECT * FROM borrowed JOIN employee ON employee.id = borrowed.employeeid WHERE employee.id = 'Reck'";

和你的代码

<?php

$sql = "SELECT * FROM borrowed JOIN employee ON employee.id = borrowed.employeeid WHERE employee.id = 'Reck'";

$result = mysqli_query($db, $sql);

while($row = mysqli_fetch_array($result)) {
  echo $row['lastname'];
}

?>