How to print the read php script in JSON format

Codes here !

Moderators: egami, macek, gesf

Post Reply
arunkumarmail2
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Mar 17, 2021 9:07 am

Wed Mar 17, 2021 9:18 am

Hello,
I would like to read the content of a DB, which is stored in MariaDB (Linux DB), and print it in JSON format.

I wrote the following code, and it works; however, I cannot print the output in JSON format.

Below is the code:
<?php

$hostname = "localhost";
$username = "user_cv";
$password = "";
$db = "cv";

$dbconnect=mysqli_connect($hostname,$username,$password,$db);

if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}

?>

<table border="1" align="center">
<tr>
<td>Reviewer Name</td>
<td>Stars</td>
<td>Details</td>
</tr>

<?php

$query = mysqli_query($dbconnect, "SELECT * FROM user_review")
or die (mysqli_error($dbconnect));

while ($row = mysqli_fetch_array($query)) {
echo
"<tr>
<td>{$row['reviewer_name']}</td>
<td>{$row['star_rating']}</td>
<td>{$row['details']}</td>
</tr>\n";

}

echo json_encode($query);

?>

Please help me fix my code, and how do I print the value in JSON format. I also wanted to print it in both a table format and JSON format to show the difference.

Thanks
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 137
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Wed Mar 17, 2021 12:56 pm

You need to store the results of your query to an array so you can output it twice; a quick way to do this is with mysqli_fetch_all. Replace the last while loop in your code with the following:

Code: Select all

// Read entire query result into single array
$output = mysqli_fetch_all($query, MYSQLI_ASSOC);

// Instead of using WHILE to loop over the query result, you can use FOREACH to loop over the returned array
foreach ($output as $row) {
echo
"<tr>
<td>{$row['reviewer_name']}</td>
<td>{$row['star_rating']}</td>
<td>{$row['details']}</td>
</tr>\n";

}

// $output is now the entire query result as an array, so json_encode will work
echo json_encode($output);
User avatar
Strider64
php-forum GURU
php-forum GURU
Posts: 421
Joined: Sat Mar 23, 2013 8:24 am
Location: Livonia, MI
Contact:

Wed Mar 17, 2021 6:28 pm

Code: Select all

$data= mysqli_fetch_all($query, MYSQLI_ASSOC);

output($data);

/*
 * After converting data array to JSON send back to javascript using
 * this function.
 */
function output($output)
{
    http_response_code(200);
    try {
        echo json_encode($output, JSON_THROW_ON_ERROR);
    } catch (JsonException) {
    }
}
Send the data back using JSON and don't forget to parse the data if you are not using FETCH. The whole point of using AJAX(Fetch) is to have it asynchronous so the user doesn't have to reload the page.
Life is a fig newton of your imagination! https://www.phototechguru.com/
arunkumarmail2
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Mar 17, 2021 9:07 am

Sun Mar 21, 2021 8:46 am

Thanks a lot guys. :)
wernereegranad75
New php-forum User
New php-forum User
Posts: 5
Joined: Tue Apr 20, 2021 7:32 pm

Tue Apr 20, 2021 9:02 pm

Hi there buddy, thank you so much for this awesome guide on "How to print the read php script in JSON format". I'm having a difficulty with this script when I started learning php and after a couple of practice I can finally do it properly. Try this toca boca game or granny game if you are looking for a new interesting game to play with your friends or random players in the game. There are lots of amazing features that both this game offers and you can get a rewards from logging in on the game.
Post Reply