Skip to content

Commit

Permalink
SQL Tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
astechedu committed Mar 3, 2024
1 parent 920dab2 commit ed8cd93
Showing 1 changed file with 80 additions and 2 deletions.
82 changes: 80 additions & 2 deletions sqlphp/developer-notes/php/corephp/APIs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

--------------------------------------------------------
--------------------------------------------------------
Keys: APIs, API with Curl,
Keys: APIs, API with Curl, Core PHP Testing Api Using PDO MySql,


--------------------------------------------------------
--------------------------------------------------------
Expand Down Expand Up @@ -144,23 +145,100 @@ if(isset($result['status'])){
?>


-----x------


-----x------
--------------------------------------------------------



>>>> Core PHP Testing Api Using PDO MySql <<<<

db.php

<?php

$host = 'localhost';
$dbname = 'phpapi';
$username = 'root';
$password = '';

try {

$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (PDOException $e) {

die("Database connection failed: " . $e->getMessage());
}

?>



//index.php

<?php
header('Content-Type:application/json');

$method = $_SERVER['REQUEST_METHOD'];

include 'db.php';

switch ($method) {
//Read Operation
case 'GET':
$sql = 'SELECT *FROM users';
$stmt = $pdo->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result);
break;

//Create Operation
case 'POST':
$data = json_decode(file_get_contents('php://input'),true);
$name = $data['name'];
$age = $data['age'];
$stmt = $pdo->prepare('INSERT INTO users(name,age) values(?,?)');
$stmt->execute([$name,$age]);
echo json_encode(['message'=>'User succesfully inserted']);
break;

//Update Operation
case 'PUT':
parse_str(file_get_contents('php://input'), $data);
$id = $data['id'];
$name = $data['name'];
$age = $data['age'];

$stmt = $pdo->prepare('UPDATE users set name=?, age=? where id=?');
$stmt->execute([$name, $age, $id]);
echo json_encode(['message' => 'User succesfully updated']);
break;

//Delete Operation
case 'DELETE':
//$data = json_decode(file_get_contents('php://input'), $data);
//parse_str(file_get_contents('php://input'), $data);

$id = $_GET['id'];

//$id = $data['id'];
$stmt = $pdo->prepare('DELETE FROM users where id=?');
$delUser = $stmt->execute([$id]);
echo json_encode(['message'=>'User Deleted succesfully']);
break;

default:
echo json_encode(['error'=>'Method not allowed']);
break;
}


?>

-----x-----

--------------------------------------------------------

0 comments on commit ed8cd93

Please sign in to comment.