Function in MySQl PDO for json [closed]

0

I'm taking the first steps in MySQL PDO and I have 2 questions in my code. The first is related to the efficiency of the functions. The second doubt is related to the return of results from a database to insert into json_encode .

Code:

Class db ( path : classes/db.php )

class db {
    private static $_instance = null;
    private $_pdo,
            $_query,
            $_error = false,
            $_results,
            $_count = 0;

    private function __construct() {
        try{
            $this->_pdo = new PDO('mysql:host=' . config::get('mysql/host') . ';dbname=' . config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
            //echo "Connected";
        }catch(PDOExeption $e) {
            die($e->getMessage());
        }
    }

    public function get_reservations($sql)
    {
        if($this->_query = $this->_pdo->prepare($sql)) {
            if($this->_query->execute()) {
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
                //print_r($result);
            }else {
                $this->_error = true;
            }
        }
        return $this;
    }

    public function read_reservations($sdate, $edate)
    {
        $sql = "SELECT seat_number, payment_status FROM tbl_reservations WHERE start_date <= '{$edate}' AND end_date >= '{$sdate}'";
        return $this->get_reservations($sql);
    }
}

Class reservations ( path : classes/reservations.php )

class reservation {
    //aqui são guardados dados em variaveis acessivel à classe toda.
    private $_db,
            $_data;

    public function __construct($user= null) {
        $this->_db = db::getInstance();
}

public function find_reservation($sdate, $edate)
{
    $data = $this->_db->read_reservations($sdate, $edate);
    if ($data->count()) {
        $this->_data = $data;
        return true;
    }else{
        echo 'SQL retornou vazio!';
    }
    return false;
}

index.php

<?php

 require_once 'core/init.php';
 $reservation = new reservation();


 $reservation->find_reservation('2017-02-02', '2017-02-20');
 print_r($reservation->data());
?>

OUTPUT :

  

db Object ( [_pdo:db:private] => PDO Object ( ) [_query:db:private] => PDOStatement Object ( [queryString] => SELECT seat_number, payment_status FROM tbl_reservations WHERE start_date <= '2017-02-20' AND end_date >= '2017-02-02' ) [_error:db:private] => [_results:db:private] => Array ( [0] => stdClass Object ( [seat_number] => 4 [payment_status] => 2 ) [1] => stdClass Object ( [seat_number] => 12 [payment_status] => 1 ) [2] => stdClass Object ( [seat_number] => 7 [payment_status] => 2 ) ) [_count:db:private] => 3 )

How do I transform the previous OUTPUT into a array like this:

[ 4 => 2, 12 => 1, 7 => 2]

to be able to do this:

echo json_encode([ 4 => 2, 12 => 1, 7 => 2]);
    
asked by anonymous 23.01.2017 / 05:12

1 answer

1

First of all I do not know what you need to do this, but anyway I could understand you're trying to turn a class into json.

To do this, try to use the get_object_vars function in conjunction with json_encode . Example:

json_encode(get_object_vars($classString));
    
23.01.2017 / 07:24