0

I have a code that have an access to my database's "comments" table. I did a connection variable and created all the stuff in order to get table. But I when save and refresh the page, it is showing this error below. What's the problem?

  <?php
    // connection
    $connection = mysqli_connect(
    $config['db']['server'],
    $config['db']['username'],
    $config['db']['password'],
    $config['db']['name']
);

if ($connection == false)
{
    echo 'Error!<br>';
    echo mysqli_connect_error();
    exit();
}
// comments
        $сomments = mysqli_query($connection, "SELECT * FROM `comments` ORDER BY `articles_id` DESC LIMIT 5");
        while ($com = mysqli_fetch_assoc($comments)) 
        {
          ?>
          <article class="article">
            <div class="article__image" style="background-image: url(https://www.gravatar.com/avatar/<?php echo md5($com['email']); ?>?s=125);"></div>
            <div class="article__info">
              <a href="/article.php?id=<?php echo $com['articles_id']; ?>"><?php echo $com['author']; ?></a>

              <div class="article__info__preview"><?php echo mb_substr(strip_tags($com['text']), 0, 100, 'utf-8') .  ' ...'; ?></div>
            </div>
          </article>
          <?php
        }
      ?>

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in W:\domains\itblog.kg\includes\sidebar.php on line 56 . What is the problem?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Sandro
  • 1
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Progman Oct 30 '19 at 22:36

1 Answers1

0

The problem is that the query ($comments = mysqli_query(...) is returning a null value. This means that there has been some problem with the query.

Try changing the code like this:

 $сomments = mysqli_query($connection, "SELECT * FROM `comments` ORDER BY `articles_id` DESC LIMIT 5");

// start new
        if (!$comments) {
               echo "Error - " . mysqli_error($connection);
        } else 
// end new

        while ($com = mysqli_fetch_assoc($comments)) 
        {
          ?>
          <article class="article">
...

(Note that you should also surround the whole while loop with braces {}, as it is the else clause, to avoid future errors. But it should work like that.)

The script should report the error it is seeing and it should allow you to fix the query.

Edit - I'd bet that the comments table does not have an articles_id column - probably it should be article_id.

  • -Thanks. I tried. But it's again showing me the same error: `Error - Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in W:\domains\itblog.kg\includes\sidebar.php on line 59` – Sandro Oct 23 '19 at 13:20
  • That's really weird. So `$comments` is null but there's no error in the `$connection` mysqli handle. Could you try adding `echo mysqli_connect_error();` right after the `mysqli_connect`? In theory that should be caught by the `if ($connection == false)` just below, but that's about the only explanation I can come up with... [Edit - and make sure that the lines are inserted between the `mysqli_query` and the `while`] – andvaranaut Oct 23 '19 at 14:26
  • I did as you said. But, the result is the same :( – Sandro Oct 24 '19 at 09:13
  • The only thing that I can think of is that something else is wrong in the sidebar.php script. Would it be possible for you to show the full code? – andvaranaut Oct 24 '19 at 10:14