Representational state transfer (REST) is a de-facto standard for a software architecture for interactive applications that typically use multiple Web services. (source: Wikipedia)
To know if you have an interactive application that needs a database, then you can use a REST server to send data from the database to the application.
REST API (PHP MySql)
The API is the URL of a web application that sends HTTP requests to GET and takes POST data.
The Application Programming Interface (API) is an application programming interface and is a set of protocols and tools for creating applications. The API allows communication between different programming languages.
In the example below I will show you the PHP procedure that allows us the API through JSON.
<?php
//connection
require 'conn.php';
header("Content-Type: application/json");
header("Acess-Control-Allow-Origin: *");
//standard select and num rows procedural PHP
$con = "SELECT * FROM accounts";
$res = mysqli_query($konekcija, $con);
$no = mysqli_num_rows($res);
//Classic IF loop that tests if "no > 0 "show data in JSON format, and if "<" or "=" then show message no data
if($no > 0)
{
$va = mysqli_fetch_all($res);
echo json_encode($va);
}
else
{
echo json_encode(array("Message" => "Values not found.", "status" => false));
}
?>
An example is a combination of PHP, MySql and XML
As we previously stated the description of the code for REST (PHP, MySql and XML) can be found at Function 7 & Grid - "F 7 JSON & XML"
<table>
<thead>
<tr>
<th>Id</th>
<th>User_id</th>
<th>Name</th>
<th>Email</a></th>
<th>Year</th>
</tr>
</thead>
<tbody>
<?php
require 'conn.php';
$sql = "SELECT * FROM xml";
$v = array();
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$id= $row['id'];
$user_id= $row['user_id'];
$name= $row['name'];
$email= $row['email'];
$year = $row['year'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $user_id; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $year; ?></td>
</tr>
<?php
array_push($v, $row);
$v = array();
$user_id = $row[1];
$name = $row[2];
$email = $row[3];
$year = $row[4];
$v[]= array (
'user_id' => $user_id,
'name' => $name,
'email' => $email,
'year' => $year
);
$v[] = $row;
}
?>
<?php
$fp = fopen('osoba.json', 'w');
fwrite($fp, json_encode($v));
fclose($fp)
?>
<?php
function createXMLfile($v){
$filePath = 'users.xml';
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('books');
for($i=0; $i< count($v); $i++){
$user_id = $v[$i]['user_id'];
$name = $v[$i]['name'];
$email = $v[$i]['email'];
$year = $v[$i]['year'];
$cha = $dom->createElement('child');
$user_id = $dom->createElement('user_id', $user_id);
$child->appendChild($user_id);
$name = $dom->createElement('name', $name);
$child->appendChild($name); $year = $dom->createElement('year', $year);
$child->appendChild($year);
$email = $dom->createElement('email', $email);
$child->appendChild($email);
$root->appendChild($child);
}
$dom->appendChild($root);
$dom->save($filePath);
}
?>
</tbody>
</table>