我是php新手,我已成功显示sqlite数据库中的数据。我要$catname
链接到一个页面,该页面显示数据库中另一个table
的数据与id
相同。以下是代码:
<?php
require_once ("db.php");
$db = new MyDb();
$sql =<<<EOF
SELECT * FROM addcategory ORDER BY catID DESC;
EOF;
$ret = $db->query($sql);
while ($row = $ret->fetchArray(SQLITE3_ASSOC)) {
$catname = $row['catname'];
$catdes = $row['catbrief'];
$catimage = $row['catpic'];
$catid = $row['catID'];
echo "<div class=\"catDescription\">
<div class=\"catname\"><p><a href='search.php?category_id=$catid'>$catname</a> </p></div>
<div class=\"catImage\"><img src='".$catimage."'></div>
<div class=\"catprof\"><p>$catdes</p></div>
</div>";
}
?>
如何使用id
与其他表中的id
显示数据
我在另一页上试过这个
<?php
$sql =<<<EOF
SELECT * FROM questions ORDER BY category_id DESC;
EOF;
$ret = $db->query($sql);
while ($row = $ret->fetchArray(SQLITE3_ASSOC)) {
$catquestion = $row['question'];
$catans = $row['answer'];
echo "<div class=\"catDescription\">
<div class=\"catname\"><p>$catquestion</p></div>
<div class=\"catprof\"><p>$catans</p></div>
</div>";
}
?>
但它显示表中的所有数据,而不是具有相似ID的数据。请问这里有什么问题以及如何解决这个问题。
答案 0 :(得分:0)
您似乎缺少相应的WHERE
子句。您的查询应该是:
$cat_id = (int)$_GET['category_id']; //cast to int to strip out sql injections
$sql =<<<EOF
SELECT * FROM questions WHERE category_id = {$cat_id} ORDER BY category_id DESC;
EOF;