Developed Simple Rest API data in array convert into JSON fetch from MySQL Database Tables.
Below is code for creating JSON Array API data fetch from MySQL database. I use this simple API to get all records entered in a database table. I developed this on localhost requires database configuration changes in the top 4 variables. It fetches all records in JSON output which can use by Ajax in the index.php page or any other page.
Create PHP Rest API Data From MySQL Database
<?php
$host = 'localhost';
$username = 'root';
$password = 'password';
$dbname = 'international_rates';
$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
$sql = $conn->query("SELECT * FROM international_rates_table");
$response = array();
while($row = $sql->fetch_assoc())
{
$remove[] = "'";
$remove[] = '"';
//$remove[] = '?';
$remove[] = chr(054);
$response[] = str_replace( $remove, "", $row );
//$response[] = mb_convert_encoding($row,'HTML-ENTITIES','UTF-8');
}
header('Content-type: application/json; charset=utf-8');
echo json_encode($response);
?>
