Created
July 11, 2017 16:11
-
-
Save alphanetEX/95bfd44fd39dfec4d9a5be1006a5ea89 to your computer and use it in GitHub Desktop.
consulta php slim
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 | |
use \Psr\Http\Message\ServerRequestInterface as Request; | |
use \Psr\Http\Message\ResponseInterface as Response; | |
$app = new \Slim\App; | |
//Obtener todos los clientes | |
$app->get('/cliente', function(Request $request, Response $response){ | |
$consulta = "SELECT * FROM clientes"; | |
try{ | |
// Instanciar la base de datos | |
$db = new db(); | |
// Conexión | |
$db = $db->conectar(); | |
$ejecutar = $db->query($consulta); | |
$clientes = $ejecutar->fetchAll(PDO::FETCH_OBJ); | |
$db = null; | |
//Exportar y mostrar en formato JSON | |
echo json_encode($clientes); | |
} catch(PDOException $e){ | |
echo '{"error": {"text": '.$e->getMessage().'}'; | |
} | |
}); | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment