title: How to encode json with multiple rows?
body: <p>Before I begin, I have looked through other examples and Q&amp;A's on multiple platforms but none of them seem to solve my problem. I am trying to return multiple rows from MySQL via a json. However, I have been unable to. The code below shows my attempt.</p>
  
  <p>I get my responses via Postman. The first <code>while</code> returns only the last entry in the database, and the <code>do-while</code> returns all entries but doesn't encode the json properly, as the json outputs <code>syntax error</code> but the html part shows all entries.</p>
  
  <pre><code>&lt;?php
      $dashboard_content_token = $_REQUEST["dashboard_content_token"];
      $token = "g4";
  
      require(cc_scripts/connect.php);
  
      $sql = "SELECT * FROM `dashboard_content`";
      $check = strcmp("$token", "$dashboard_content_token");
      $statement = mysqli_query($con, $sql);
      if (check) {
          $rows = mysqli_fetch_assoc($statement);
          if (!$rows) {
              echo "No results!";
          } else {
                while ($rows = mysqli_fetch_assoc($statement)) {
                  $news_id = $rows['news_id'];
                  $image_url = $rows['image_url'];
                  $news_title = $rows['news_title'];
                  $news_description = $rows['news_description'];
                  $news_article = $rows['news_article'];
  
                  $result['dashboard content: '][] = array('news_id' =&gt; $news_id, 'image_url' =&gt; $image_url, 'news_title' =&gt; $news_title, 'news_description' =&gt; $news_description, 'news_article' =&gt; $news_article); 
  
                  echo json_encode($result);
          }
          // do {
                  // $news_id = $rows['news_id'];
                  // $image_url = $rows['image_url'];
                  // $news_title = $rows['news_title'];
                  // $news_description = $rows['news_description'];
                  // $news_article = $rows['news_article'];
  
                  // $result['dashboard content: '][] = array('news_id' =&gt; $news_id, 'image_url' =&gt; $image_url, 'news_title' =&gt; $news_title, 'news_description' =&gt; $news_description, 'news_article' =&gt; $news_article); 
  
                  // echo json_encode($result);
          //     } while ($rows = mysqli_fetch_assoc($statement));
  
  
          mysqli_free_result($statement);
      }
  }
  ?&gt;
  </code></pre>