Jump to content

Code returning empty objects


TechnoDiver

Recommended Posts

Hi Phreaks, I hope you've all made it through the weekend with sound body and mind.

The following 2 methods have been working together harmoniously for quite a while:

<?php

public function action($action, $table, $where = [], $rule = "") {
    if(count($where) === 3) {
        $operators = array('=', '<', '>', '<=', '<=');

        $field = $where[0];
        $operator = $where[1];
        $value = $where[2];

        if(in_array($operator, $operators)) {
            if($rule == "") {
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
            } else {
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ? {$rule}";
            }

            if(!$this->query($sql, array($value))->error()) {
                return $this;
            }
        }
      return false;
    }
  
  public function get($table, $where = [], $rule = "", $column = "*") {
        return $this->action("SELECT {$column}", $table, $where, $rule);
  }

My original belief was that it was easily extensible. I'm now to the point I'm trying to do that and receiving behaviour that I don't understand. If the action() method becomes thus ->

<?php

public function action($action, $table, $where = [], $rule = "") {
    if(count($where) === 3) {
        $operators = array('=', '<', '>', '<=', '<=');

        $field = $where[0];
        $operator = $where[1];
        $value = $where[2];

        if(in_array($operator, $operators)) {
            if($rule == "") {
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
            } else {
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ? {$rule}";
            }

            if(!$this->query($sql, array($value))->error()) {
                return $this;
            }
        }
    } else { 

        $sql = "{$action} FROM {$table}";

        echo "this <br>";
        return $this;
    }

    echo "false <br>";
    return false;
}

with the added else{} statement to handle queries without WHERE conditions, and call it with just the $table parameter ->

<?php

public function get() {

        // $table = "posts";
        $table = "categories";
        $field = "*";

        $query = $this->_db->get($table);
        // print_r($query);
        var_dump($query);
    }

//normally, using conditions, it would look like this: $query = $this->_db->get($table, array($field, "=", $value); 

It returns:

Quote

this
object(DB)#3 (5) { ["_pdo":"DB":private]=> object(PDO)#4 (0) { } ["_query":"DB":private]=> NULL ["_error":"DB":private]=> bool(false) ["_results":"DB":private]=> NULL ["_count":"DB":private]=> int(0) }

which we can see is picking up my else{} statement as it should but is returning this empty object. Could someone explain this to me? thank you

Edited by TechnoDiver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.