0% found this document useful (0 votes)
2 views4 pages

API Sample

The document outlines a REST API development for managing student profiles using PHP and PDO for MySQL database interactions. It includes classes for database connection, application configuration, and student record operations such as updating and searching records. Error handling is implemented to manage exceptions and return appropriate JSON responses for API requests.

Uploaded by

Jayver Segovia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

API Sample

The document outlines a REST API development for managing student profiles using PHP and PDO for MySQL database interactions. It includes classes for database connection, application configuration, and student record operations such as updating and searching records. Error handling is implemented to manage exceptions and return appropriate JSON responses for API requests.

Uploaded by

Jayver Segovia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

REST-API DEVELOPMENT

Site-Map

Update Profiles’ Record

Search Profiles’ Record

1|3
Filename : [Link]

class PdoMySql
{

private static function OpenDatabase($DBase)


{ $Options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
PDO::ATTR_EMULATE_PREPARES => false];
$Connection = new PDO("mysql:host=". $DBase["Host"] . ";port=" . $DBase["Port"] . ";dbname=" . $DBase["DBName"] .
";charset=utf8mb4;", $DBase["Username"], $DBase["Password"], $Options);
return $Connection;
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
public static function ExecuteDML_Query($DBase, $Query, $Parameter=null)
{ $Result="";
set_error_handler(function($errno, $errstr, $errfile, $errline ){
throw new ErrorException($errstr, $errno, 0, $errfile, $errline); });

try { $Connection= Self::OpenDatabase($DBase);


$Statement=$Connection->prepare($Query);
if($Parameter==null){$Statement->execute();}
else{$Statement->execute($Parameter);}

$Result= json_encode($Statement->fetchall(PDO::FETCH_ASSOC), JSON_UNESCAPED_UNICODE);


} catch(PDOException $Exception) {return json_encode(Array("Status"=> "Error: " . $Exception->getMessage()),
JSON_UNESCAPED_UNICODE);}

$Result= str_replace("\\", "", $Result);


$Result= str_replace("\"{", "{", $Result);
$Result= str_replace("}\"", "}", $Result);

return $Result;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
}

?>

=====================================================================================

Filename : [Link]

<?php
class Application{
public static $DBase = array("Host" => "localhost",
"Port" => "3306",
"Username" => "root",
"Password" => "",
"DBName" => "Student");

public static $Server = array("TimeZone" => "Asia/Manila");

?>

=====================================================================================

2|3
Filename : [Link]

<?php
require_once ("[Link]");

$Method = strtoupper($_SERVER['REQUEST_METHOD']);
$Data = file_get_contents('php://input');
$ObjData=json_decode($Data);

if(strtoupper($Method)=="POST")
{
if(strtoupper($ObjData->Request)=="UPDATE") {Student::UpdateRecord($ObjData->Record);}
else if(strtoupper($ObjData->Request)=="SEARCH") {Student::SearchRecord($ObjData->Record);}
else{ echo json_encode(Array("Status"=> "Error: Service request is not valid."), JSON_UNESCAPED_UNICODE);}

} else{ echo json_encode(Array("Status"=> "Error: POST method is required in the process."),


JSON_UNESCAPED_UNICODE);}
?>

=====================================================================================

Filename : [Link]

<?php

require_once ("../[Link]");
require_once ("../[Link]");

class Student{
static function UpdateRecord($Record) {
set_error_handler(function($errno, $errstr, $errfile, $errline ){
throw new ErrorException($errstr, $errno, 0, $errfile, $errline); });
try{
$MyRecord = Array($Record->IDNO, $Record->FName, $Record->GName, $Record->MName,
$Record->Email_Add, $Record->Mobile_No, $Record->Birthdate, $Record->Address);

$Result = PdoMySQL::ExecuteDML_Query(Application::$DBase, "Call Profile_Update(?,?,?,?,?,?,?,?)", $MyRecord);


if(Trim($Result)!=""){
$Result = json_decode($Result) ;
$Result = $Result[0]->Record_Status;
echo json_encode(Array("Status"=> "The requested service has been successfully processed. $Result"),
JSON_UNESCAPED_UNICODE);
}else{echo json_encode(Array("Status"=> "Error: Request has failed. The server has encountered an error."),
JSON_UNESCAPED_UNICODE);}
}catch(ErrorException $e){echo json_encode(Array("Status"=> "Error: Request has failed. The server cannot
interpret the requested service."),
JSON_UNESCAPED_UNICODE);}

//======================================================================================================

static function SearchRecord($SearchKey) {


set_error_handler(function($errno, $errstr, $errfile, $errline ){
throw new ErrorException($errstr, $errno, 0, $errfile, $errline); });

try{ //date_default_timezone_set(Application::$Server["TimeZone"]);
//$Timestamp=date('Y-m-d H:i:s');

$Result = PdoMySQL::ExecuteDML_Query(Application::$DBase, "Call Profile_Search(?)",[$SearchKey]);


if(Trim($Result)!=""){
$Result = json_decode($Result) ;
echo json_encode(Array("Status"=> "The requested service has been successfully
processed." , "Record" => $Result[0]), JSON_UNESCAPED_UNICODE);
}else{echo json_encode(Array("Status"=> "Error: Request has failed. The server has encountered an
error."), JSON_UNESCAPED_UNICODE);}

}catch(ErrorException $e){echo json_encode(Array("Status"=> "Error: Request has failed. The


server cannot interpret the requested service."), JSON_UNESCAPED_UNICODE);}

//======================================================================================================

3|3
}

?>

4|3

You might also like