Php CRUD API Sample
<?php | |
define('DB_HOST', 'localhost'); | |
define('DB_USER', 'YOUR_USER'); | |
define('DB_PASSWORD', 'YOUR_PASSWORD'); | |
define('DB_DATABASE', 'YOUR_DB'); | |
define('ANDROID_API','YOUR API TOKEN'); | |
define('ANDROID_APN_URL','https://fcm.googleapis.com/fcm/send'); | |
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); | |
if(!$link) | |
{ | |
die('Failed to connect to server: ' . mysql_error()); | |
} | |
$db = mysql_select_db(DB_DATABASE); | |
if(!$db) | |
{ | |
die("Unable to select database"); | |
} | |
?> |
<?php | |
include("config.php"); | |
$Name = getIfSet($_REQUEST['name']); | |
$Email = getIfSet($_REQUEST['email']); | |
$Contact = getIfSet($_REQUEST['contact']); | |
$Password = getIfSet($_REQUEST['password']); | |
$DeviceToken = getIfSet($_REQUEST['token']); | |
$Latitude = getIfSet($_REQUEST['latitude']); | |
$Longitude = getIfSet($_REQUEST['longitude']); | |
$Status = 0; //0 Assigned and 1 not assigned | |
$UserId = 0; | |
if ($Name == null || $Email == null || $Contact == null || $Password == null || $DeviceToken == null || $Latitude == null || $Longitude == null) { | |
$result = array( | |
"Status" => 0, | |
"Message" => 'No parameters found!',"Data"=> null); | |
echo json_encode($result); | |
return; | |
} | |
$InsertQuery = "INSERT INTO Rider (Name,Email,Password,Contact,DeviceToken,Latitude,Longitude,Status) values('".$Name."','".$Email."','".$Password."','".$Contact."','".$DeviceToken."','".$Latitude."','".$Longitude."',0)"; | |
//ECHO "</BR> User Insert Query: ".$InsertQuery."</BR>"; | |
if(mysql_query($InsertQuery)){ | |
$UserId = mysql_insert_id(); | |
$SelectQuery = "SELECT Id,Name,Email,Password,Contact,DeviceToken,Latitude,Longitude,Status FROM Rider where Id=".$UserId; | |
//echo "</BR> User SelectQuery: ".$SelectQuery."</BR>"; | |
$query = mysql_query($SelectQuery); | |
$row = mysql_fetch_row($query); | |
$array = array("Id" => (int)$row[0],"Name" => $row[1],"Email" => $row[2],"Password" => $row[3],"Contact" => $row[4],"DeviceToken" => $row[5],"Latitude" => $row[6],"Longitude" => $row[7],"Status" => (int)$row[8]); | |
$result = array("Status"=>1,"Message" => 'Registeration successfully',"Data" => $array); | |
echo json_encode($result); | |
}else{ | |
$result = array( | |
"Status" => 0, | |
"Message" => 'Unable to register problem encountered at server',"Data"=> null); | |
echo json_encode($result); | |
} | |
//Get value from $_REQUEST | |
function getIfSet(&$value, $default = null) | |
{ | |
return isset($value) ? $value : $default; | |
} | |
?> |
<?php | |
include("config.php"); | |
$Id = getIfSet($_REQUEST['id']); | |
if ($Id == null) { | |
$result = array( | |
"Status" => 0, | |
"Message" => 'Required Id to delete',"Data"=> null); | |
echo json_encode($result); | |
return; | |
} | |
$sql="DELETE FROM Rider WHERE Id=".$Id; | |
if(mysql_query($sql)){ | |
$NewArray = array( | |
"Status" => 1, | |
"Message" => 'Rider successfully deleted', | |
"Data"=> null); | |
}else{ | |
$NewArray = array( | |
"Status" => 0, | |
"Message" => 'Unable to delete Rider problem encountered at server', | |
"Data"=> null); | |
} | |
echo json_encode($NewArray); | |
//Get value from $_REQUEST | |
function getIfSet(&$value, $default = null) | |
{ | |
return isset($value) ? $value : $default; | |
} | |
?> |
<?php | |
include("config.php"); | |
$Result_array = array(); | |
$query = mysql_query("SELECT Id,Name,Email,Password,Contact,DeviceToken,Latitude,Longitude,Status FROM Rider"); | |
while($Sqlrow=mysql_fetch_array($query,MYSQL_ASSOC)) | |
{ | |
$array = array("Id" => (int)$Sqlrow["Id"],"Name" => $Sqlrow["Name"],"Email" => $Sqlrow["Email"],"Password" => $Sqlrow["Password"],"Contact" => $Sqlrow["Contact"],"DeviceToken" => $Sqlrow["DeviceToken"],"Latitude" => $Sqlrow["Latitude"],"Longitude" => $Sqlrow["Longitude"],"Status" => (int)$Sqlrow["Status"]); | |
$Result_array[]=$array; | |
} | |
if(empty($Result_array)){ | |
//Array list is empty no record found | |
$result = array("Status"=>0,"Message" => 'No Rider\'s found!',"Data" => $Result_array); | |
}else{ | |
$result = array("Status"=>1,"Message" => 'Rider\'s found!',"Data" => $Result_array); | |
} | |
echo json_encode($result); | |
//Get value from $_REQUEST | |
function getIfSet(&$value, $default = null) | |
{ | |
return isset($value) ? $value : $default; | |
} | |
?> |
<?php | |
include("config.php"); | |
$Email = getIfSet($_REQUEST['email']); | |
$Password = getIfSet($_REQUEST['password']); | |
if ($Email == null || $Password == null) { | |
$result = array( | |
"Status" => 0, | |
"Message" => 'No parameters found!',"Data"=> null); | |
echo json_encode($result); | |
return; | |
} | |
$SelectQuery = "SELECT Id,Name,Email,Password,Contact,DeviceToken,Latitude,Longitude,Status FROM Rider where Email='".$Email."' AND Password='".$Password."'"; | |
$query = mysql_query($SelectQuery); | |
if(mysql_num_rows($query) > 0){ | |
$row = mysql_fetch_row($query); | |
$data = array("Id" => (int)$row[0],"Name" => $row[1],"Email" => $row[2],"Password" => $row[3],"Contact" => $row[4],"DeviceToken" => $row[5],"Latitude" => $row[6],"Longitude" => $row[7],"Status" => (int)$row[8]); | |
$result = array("Status"=>1,"Message" => 'Login success',"Data" => $data); | |
echo json_encode($result); | |
}else{ | |
$result = array( | |
"Status" => 0, | |
"Message" => 'Invalid credentials',"Data"=> null); | |
echo json_encode($result); | |
} | |
//Get value from $_REQUEST | |
function getIfSet(&$value, $default = null) | |
{ | |
return isset($value) ? $value : $default; | |
} | |
?> |
<?php | |
include("config.php"); | |
$Id = getIfSet($_REQUEST['id']); | |
$Name = getIfSet($_REQUEST['name']); | |
$Email = getIfSet($_REQUEST['email']); | |
$Contact = getIfSet($_REQUEST['contact']); | |
$Password = getIfSet($_REQUEST['password']); | |
$DeviceToken = getIfSet($_REQUEST['token']); | |
$Latitude = getIfSet($_REQUEST['latitude']); | |
$Longitude = getIfSet($_REQUEST['longitude']); | |
$Status = getIfSet($_REQUEST['status']); //0 Assigned and 1 not assigned | |
$hasValues = false; | |
if ($Id == null) { | |
$result = array( | |
"Status" => 0, | |
"Message" => 'Required Id to update',"Data"=> null); | |
echo json_encode($result); | |
return; | |
} | |
if (!empty($Name) || !empty($Email) || !empty($Contact) || !empty($Password) || !empty($DeviceToken) || !empty($Latitude) || !empty($Longitude) || !empty($Status)) { | |
$hasValues = true; | |
} | |
if ($hasValues == false) { | |
$result = array( | |
"Status" => 0, | |
"Message" => 'No parameters found!',"Data"=> null); | |
echo json_encode($result); | |
return; | |
} | |
$UpdateQuery = "UPDATE Rider SET ".getSetValues()." WHERE Id=".$Id; | |
if(mysql_query($UpdateQuery)){ | |
$SelectQuery = "SELECT Id,Name,Email,Password,Contact,DeviceToken,Latitude,Longitude,Status FROM Rider where Id=".$Id; | |
$query = mysql_query($SelectQuery); | |
$row = mysql_fetch_row($query); | |
$array = array("Id" => (int)$row[0],"Name" => $row[1],"Email" => $row[2],"Password" => $row[3],"Contact" => $row[4],"DeviceToken" => $row[5],"Latitude" => $row[6],"Longitude" => $row[7],"Status" => (int)$row[8]); | |
$result = array("Status"=>1,"Message" => 'Rider updated successfully',"Data" => $array); | |
echo json_encode($result); | |
}else{ | |
$result = array( | |
"Status" => 0, | |
"Message" => 'Unable to update rider problem encountered at server',"Data"=> null); | |
echo json_encode($result); | |
} | |
function getSetValues() | |
{ $set = ""; | |
global $Name,$Email,$Password,$Contact,$DeviceToken,$Latitude,$Longitude,$Status; | |
if ($Name != null) { | |
$set .= "Name='".$Name."',"; | |
} | |
if ($Email != null) { | |
$set .= "Email='".$Email."',"; | |
} | |
if ($Password != null) { | |
$set .= "Password='".$Password."',"; | |
} | |
if ($Contact != null) { | |
$set .= "Contact='".$Contact."',"; | |
} | |
if ($DeviceToken != null) { | |
$set .= "DeviceToken='".$DeviceToken."',"; | |
} | |
if ($Latitude != null) { | |
$set .= "Latitude='".$Latitude."',"; | |
} | |
if ($Longitude != null) { | |
$set .= "Longitude='".$Longitude."',"; | |
} | |
if ($Status != null) { | |
$set .= "Status=".$Status.","; | |
} | |
return rtrim($set,','); | |
} | |
//Get value from $_REQUEST | |
function getIfSet(&$value, $default = null) | |
{ | |
return isset($value) ? $value : $default; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Very good bro
Checkout