Created
November 25, 2019 21:35
-
-
Save parzibyte/95d173fb79c7683c1880252318577ca3 to your computer and use it in GitHub Desktop.
Servidor PHP para consultas Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$contraseña = ""; | |
$usuario = "root"; | |
$nombre_base_de_datos = "mascotas"; | |
try { | |
return new PDO('mysql:host=localhost;dbname=' . $nombre_base_de_datos, $usuario, $contraseña); | |
} catch (Exception $e) { | |
echo "Ocurrió algo con la base de datos: " . $e->getMessage(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Access-Control-Allow-Origin: http://localhost:4200"); | |
header("Access-Control-Allow-Methods: DELETE"); | |
$metodo = $_SERVER["REQUEST_METHOD"]; | |
if ($metodo != "DELETE" && $metodo != "OPTIONS") { | |
exit("Solo se permite método DELETE"); | |
} | |
if (empty($_GET["idMascota"])) { | |
exit("No hay id de mascota para eliminar"); | |
} | |
$idMascota = $_GET["idMascota"]; | |
$bd = include_once "bd.php"; | |
$sentencia = $bd->prepare("DELETE FROM mascotas WHERE id = ?"); | |
$resultado = $sentencia->execute([$idMascota]); | |
echo json_encode($resultado); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Access-Control-Allow-Origin: http://localhost:4200"); | |
if (empty($_GET["idMascota"])) { | |
exit("No hay id de mascota"); | |
} | |
$idMascota = $_GET["idMascota"]; | |
$bd = include_once "bd.php"; | |
$sentencia = $bd->prepare("select id, nombre, raza, edad from mascotas where id = ?"); | |
$sentencia->execute([$idMascota]); | |
$mascota = $sentencia->fetchObject(); | |
echo json_encode($mascota); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Access-Control-Allow-Origin: http://localhost:4200"); | |
$bd = include_once "bd.php"; | |
$sentencia = $bd->query("select id, nombre, raza, edad from mascotas"); | |
$mascotas = $sentencia->fetchAll(PDO::FETCH_OBJ); | |
echo json_encode($mascotas); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Access-Control-Allow-Origin: http://localhost:4200"); | |
header("Access-Control-Allow-Headers: *"); | |
$jsonMascota = json_decode(file_get_contents("php://input")); | |
if (!$jsonMascota) { | |
exit("No hay datos"); | |
} | |
$bd = include_once "bd.php"; | |
$sentencia = $bd->prepare("insert into mascotas(nombre, raza, edad) values (?,?,?)"); | |
$resultado = $sentencia->execute([$jsonMascota->nombre, $jsonMascota->raza, $jsonMascota->edad]); | |
echo json_encode([ | |
"resultado" => $resultado, | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Access-Control-Allow-Origin: http://localhost:4200"); | |
header("Access-Control-Allow-Methods: PUT"); | |
header("Access-Control-Allow-Headers: *"); | |
if ($_SERVER["REQUEST_METHOD"] != "PUT") { | |
exit("Solo acepto peticiones PUT"); | |
} | |
$jsonMascota = json_decode(file_get_contents("php://input")); | |
if (!$jsonMascota) { | |
exit("No hay datos"); | |
} | |
$bd = include_once "bd.php"; | |
$sentencia = $bd->prepare("UPDATE mascotas SET nombre = ?, raza = ?, edad = ? WHERE id = ?"); | |
$resultado = $sentencia->execute([$jsonMascota->nombre, $jsonMascota->raza, $jsonMascota->edad, $jsonMascota->id]); | |
echo json_encode($resultado); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment