0

Hello i have recently been making an attempt at creating a forum and am currently working on my forum index page. I have only recently started getting into PHP and have done it for around 2 weeks.

I dont understand what exactly i need to fix the error from coming up, but its obviously in relation to my use of mysqli_num_rows and mysqli_fetch_assoc. i find it difficult to understand why it doesn't like the boolean response. If anyone could explain it i would appreciate it.The code stops at the second error message "No categories have been added by admin" and The code below is what i have so far:

<?php
include 'connect.php';
include 'header.php';


$sql = "SELECT
      cat_id,
      cat_name,
      cat_description,
      FROM
      categories";


$result =mysqli_query($link, $sql);
    if(!isset($result))
    {
        echo 'Error while selecting from database.Please try again later.';
    }
    else
    {
if(mysqli_num_rows($result)== 0)
     {
         echo 'No categories have been added by Admin';
     }
 else
 {
     echo '<table border="1">
          <tr>
          <th>Category</th>
          <th>Last Topic</th>
          </tr>';
 while ($row =mysqli_fetch_assoc($result))
 {
     echo '<tr>';
         echo '<td class="leftpart">';
             echo '<h3><a href="category.php?id">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'];
         echo '</td>';
         echo '<td class="rightpart">';
                     echo '<a href="topic.php?id=">Topic Subject</a> at 10-10';
         echo '</td>';
     echo '</tr>';
 }
 }
    }

include 'footer.php';
?>
Richard
  • 9
  • 1
  • 6
  • 1
    Your query is failing, probably due to the trailing `,` at the end of `cat_description,`. – Jonnix Jun 14 '16 at 16:47
  • 1
    Your `$result` will always be set. Use `!empty()` instead to check for false result on failures. – aynber Jun 14 '16 at 16:47
  • Are you expecting a boolean to be returned from that query? Per the manual `Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object.` So your query is failing, http://php.net/manual/en/mysqli.error.php. – chris85 Jun 14 '16 at 16:50
  • Thank you for all the help :)!! – Richard Jun 14 '16 at 16:51

1 Answers1

1

You have a comma before FROM in your select statement.

chris85
  • 23,846
  • 7
  • 34
  • 51
Peter VARGA
  • 4,780
  • 3
  • 39
  • 75