-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
27 lines (22 loc) · 875 Bytes
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
//PDO is a extension which defines a lightweight, consistent interface for accessing databases in PHP.
$db=new PDO('mysql:dbname=php-api;host=localhost;','root','');
//here prepare the query for analyzing, prepared statements use less resources and thus run faster
$row=$db->prepare('select * from users');
$row->execute();//execute the query
$json_data=array();//create the array
foreach($row as $rec)//foreach loop
{
$json_array['id']=$rec['id'];
$json_array['username']=$rec['username'];
$json_array['password']=$rec['password'];
$json_array['level']=$rec['level'];
$json_array['fullname']=$rec['fullname'];
//here pushing the values in to an array
array_push($json_data,$json_array);
}
//built in PHP function to encode the data in to JSON format
echo json_encode($json_data);
echo "messege :Show data user succes,";
echo "code : 200";
?>