PDO选择Where子句

时间:2017-05-08 17:43:04

标签: sql select pdo view

嘿,我有错误SQLSTATE 42000,你能帮帮我。

我有2个表和1个视图。

第一个表是Category Table

+--+--------+
|id|category|
+--+--------+
|1 |bird    |
|2 |fish    |
+--+--------+

然后我有动物表:

+--+---+-------+
|id|cat|animals|
+--+---+-------+
|1 |1  |Eagle  |
|2 |1  |Crow   |
|3 |2  |Shark  |
|4 |2  |Salmon |
+--+---+-------+

然后我创建视图以加入2表

+--+----+-------+
|id|cat |anm    |
+--+----+-------+
|1 |bird|Eagle  |
|2 |bird|Crow   |
|3 |fish|Shark  |
|4 |fish|Salmon |
+--+----+-------+

然后,为了显示来自animal_view的选定ID,我使用了以下代码:

<table>
<?php
$id=$_GET['id'];
$stmt=$db->prepare("SELECT * FROM animals_view WHERE id = $id");
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $data){
?>
<tr>
<td>Category:</td>
<td><?php echo $data['cat];?></td>
</tr>

<tr>
<td>Animals:</td>
<td><?php echo $data['anm'];?></td>
</tr>
<?php }; ?>
</table>

0 个答案:

没有答案