我有一张表新闻:
- id_news - 主键
- 标题
- 含量
- 日期
-category - 外键
然后是类别表,其中包含:
- id_category - 主键
- 描述
我试图在表格中显示我的类别描述但它不起作用,但我的其他数据正在运作。
我是这样尝试的:
$readNews = $pdo->prepare("SELECT * FROM news ORDER BY date DESC");
$readNews ->execute();
$resultReadNews = $readNews ->rowCount();
if(!$resultReadNews >= 1)
{
echo 'There are now news yet';
}
else
{
echo '<table>';
echo ' <tr>';
echo ' <td>Title:</td>';
echo ' <td>Content:</td>';
echo ' </tr>';
while ($resultReadNews = $readNews ->fetch(PDO::FETCH_ASSOC))
{
$readCat= $pdo->prepare("SELECT * FROM categories, newsWHERE id_category = ?");
$readCat->bindValue(1,$resultReadNews ['category']);
$readCat->execute();
$resultReadCat = $readCat->fetch(PDO::FETCH_ASSOC);
$rowsCat = $readCat->rowCount();
echo '<tr>';
echo '<td>'.$resultReadNews ['title'].'</td>';
echo '<td>'.$resultReadNews ['content'].'</td>';
echo '<td>'.$resultReadCat ['description'].'</td>';
}
你看到问题出在哪里了吗?
答案 0 :(得分:1)
您选择的列名称错误。你必须改变这一行
echo '<td>'.$resultReadCat ['title'].'</td>';
这一行:
echo '<td>'.$resultReadCat ['description'].'</td>';
答案 1 :(得分:0)
您选择类别的代码是错误的。您尝试从两个表中选择描述,
$readCat= $pdo->prepare("SELECT description FROM categories,news WHERE id_category= ? AND id_category = category");
$readCat->bindValue(1,$resultReadNews ['category']);
$readCat->execute();
$resultReadCat = $readCat->fetch(PDO::FETCH_ASSOC);
$rowsCat = $readCat->rowCount();
但你必须像这样:
$readCat= $pdo->prepare("SELECT description FROM categories WHERE id_category= ?");
$readCat->bindValue(1, $resultReadNews ['category']);
$readCat->execute();
$resultReadCat = $readCat->fetch(PDO::FETCH_ASSOC);
$rowsCat = $readCat->rowCount();