Question

Im have a table News with:

-id_news - Primary Key

-title

-content

-date

-category - Foreign Key

And then a Categories table with:

-id_category - Primary Key

-description

And Im trying to show my categories description in a table and its not working, but my other data is working.

Im trying like this:

$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>';
}

Do you see where the problem is?

Was it helpful?

Solution 2

Your code for selecting categories is wrong. You try to select description from two tables, you have

$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();

But you must have smth like this:

$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();

OTHER TIPS

You are selecting the wrong column name. You have to change this row

echo '<td>'.$resultReadCat ['title'].'</td>';

for this row:

echo '<td>'.$resultReadCat ['description'].'</td>';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top