0

I'm learning REST API basic create, read, delete and update using pure PHP with no framework (i have no prior knowledge of REST API and this is my first time trying to get into it and learn it) and i have a strange problem with GET request not returning results with one property on and off when reading data,

read.php

<?php
  // Headers
  header('Access-Control-Allow-Origin: *');
  header('Content-Type: application/json');

  include_once '../../config/Database.php';
  include_once '../../models/Capteur.php';

  // Instantiate DB & connect
  $database = new Database();
  $db = $database->connect();

  // Instantiate capteur object
  $capteur = new Capteur($db);

  // Capteur read query
  $result = $capteur->read();

  // Get row count
  $num = $result->rowCount();

  // Check if any categories
  if($num > 0) {
        // Cat array
        $cap_arr = array();
        $cap_arr['data'] = array();

        while($row = $result->fetch(PDO::FETCH_ASSOC)) {
          extract($row);

          $cap_item = array(
            'id' => $id,
            'code_capteur' => $code_capteur,
            'etat' => $etat,
            'etab' => $etab,
            'created_at' => $created_at,
            'updated_at' => $updated_at
          );

          // Push to "data"
          array_push($cap_arr['data'], $cap_item);
        }

        // Turn to JSON & output
        echo json_encode($cap_arr);

  } else {
        // No Categories
        echo json_encode(
          array('message' => 'No Categories Found')
        );
  }

when i remove 'etab' => $etab it outputs data fine

This is my class: Capteur.php

<?php
  class Capteur {
    // DB stuff
    private $conn;
    private $table = 'capteurs';

    // Post Properties
    public $id;
    public $code_capteur;
    public $etat;
    public $etab;
    public $created_at;
    public $updated_at;

    // Constructor with DB
    public function __construct($db) {
      $this->conn = $db;
    }

    // Get Posts
    public function read() {
      // Create query
      $query = 'SELECT *  FROM ' . $this->table . '
                                ORDER BY
                                  created_at DESC';

      // Prepare statement
      $stmt = $this->conn->prepare($query);

      // Execute query
      $stmt->execute();

      return $stmt;
    }
    }
    ?>

this is my database structure: enter image description here

output: enter image description here

Amine
  • 901
  • 2
  • 14
  • 33
  • 1) Check the return status, is it 500 or 200? 2) Check your log 3) try var_dump() data before the echo, make sure the data is there. –  Mar 07 '19 at 23:52
  • what status do you get in response? is there any error? did you check logs or enable errors from php.ini? – devnull Ψ Mar 07 '19 at 23:52
  • I edited original post with output – Amine Mar 07 '19 at 23:55
  • @catcon it's 200 – Amine Mar 07 '19 at 23:56
  • try to dump like `print_r($etab);exit;` or `var_dump($etab);exit` check what it returns – devnull Ψ Mar 07 '19 at 23:59
  • nope got nothing @devnullΨ – Amine Mar 08 '19 at 00:00
  • 1
    thats strange. from where you get `$etab` variable? have you enabled display errors? does it reach `echo json_encode($cap_arr);` ? put dump before it and check what `$cap_arr` contains. maybe problem somewhere else – devnull Ψ Mar 08 '19 at 00:15
  • Hey, I think you should use `$cap_arr['data']` on `json_encode` instead of `$cap_arr` only. It's obviously different variable. – Roshan Mar 08 '19 at 00:42
  • I would strongly discourage **ever** using `extract()`. For one, it presents a security risk and two, you don't know what variables you're going to declare with it – Phil Mar 08 '19 at 01:25
  • You can replace your entire loop with `echo json_encode(['data' => $result->fetchAll(PDO::FETCH_ASSOC)]);`. Also, make sure [PDO is set to throw exceptions](https://stackoverflow.com/a/52324601/283366) and that you will be able to see them if they happen. See [How to get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – Phil Mar 08 '19 at 01:31

0 Answers0