mysqli get multiple rows json_encode code example

Example: return multiple rows from mysqli php and encode JSON

<?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 {

          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' => $news_id, 'image_url' => $image_url, 'news_title' => $news_title, 'news_description' => $news_description, 'news_article' => $news_article); 


          } while ($rows = mysqli_fetch_assoc($statement));

        mysqli_free_result($statement);
        echo json_encode($result);
    }
}
?>

Tags:

Php Example