Mysql PDO function for json

Asked

Viewed 168 times

0

I’m taking the first steps in MySQL PDO and I have 2 doubts in my code. A first related to the efficiency of the functions. The second doubt is related to the return of results of a database to insert into a 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 OUTPUT earlier in a array in this respect:

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

to be able to do this:

echo json_encode([ 4 => 2, 12 => 1, 7 => 2]);

1 answer

1

First I don’t know what your need to do this, but anyway from what I could understand you’re trying to turn a class into json.

To do this try using the function get_object_vars together with the json_encode. Example:

json_encode(get_object_vars($classString));
  • didn’t work :/

  • 1

    What is your need to print_r the PDO Class?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.