Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require_once('DBConnection.php');
- define("DB_HOST", "localhost");
- define("DB_USER", "placidojrc");
- define("DB_PASS", "");
- define("DB_NAME", "proyecto_fitness");
- $app->get('/usuarios', function () {
- $response = array();
- $dbc = new DBConnection();
- $sql="SELECT 'nombre' FROM usuarios";
- $rs=$dbc->getQuery($sql);
- $profile=$rs->fetch();
- echoResponse(200, $response);
- });
- <?php
- class DBConnection{
- private $host = DB_HOST;
- private $db = DB_NAME;
- private $user = DB_USER;
- private $pass = DB_PASS;
- private $dbh;
- private $error=null;
- public function __construct(){
- $options = array(
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
- );
- $dsn="mysql:host=" . $this->host . ";dbname=" . $this->db . ";";
- try{
- $this->dbh=new PDO($dsn, $this->user, $this->pass, $options);
- }
- catch(PDOException $ex){
- $this->error=$ex->getMessage();
- }
- }
- public function __toString(){
- return $this->error;
- }
- public function __destruct(){
- $dbh=null;
- }
- public function getError(){
- return $this->error;
- }
- public function runQuery($query){
- try{
- return $this->dbh->exec($query);
- }
- catch(PDOException $ex){
- $this->error=$ex->getMessage();
- return "Error";
- }
- }
- public function getQuery($query){
- try{
- $sentencia = $this->dbh->query($query);
- $sentencia->setFetchMode(PDO::FETCH_ASSOC);
- return $sentencia;
- }
- catch(PDOException $ex){
- $this->error=$ex->getMessage();
- return "Error";
- }
- }
- }?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement