A
Anonymous
Guest
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
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