1

I am trying to extend the function of this tutorial on youtube, and I have run into some issues. Here is the particular chunk of code I am currently struggling with:

public function listProjects() {
    $projects = DB::getInstance()->get('projects', array('projectStatus', '=', 'active'));
    if($projects->count()) {
        echo 'This query senses rows, but returns data it does not';
        foreach($projects as $project) {
            echo $project->projectName;
        }
    }
}

This is a method of my "Project" class, and it uses methods from the DB class, with relevant code here:

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')); 
    } catch(PDOException $e){
        die($e->getMessage());
    }
}


public static function getInstance() {
    if(!isset(self::$_instance)){
        self::$_instance = new DB();
    }
    return self::$_instance;
}


public function query($sql, $params = array()){
    $this->_error = false;
    if($this->_query = $this->_pdo->prepare($sql)) {
        $x = 1;
        if(count($params)) {
            foreach($params as $param) {
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }
        if($this->_query->execute()) {
            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        } else {
            $this->_error = true;
        }
    }
    return $this; 
}


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

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

        if(in_array($operator, $operators)) {
            $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
            if(!$this->query($sql, array($value))->error()) {
                return $this;
            }
        }

    } return $this;

}


public function get($table, $where) {
    return $this->action('SELECT *', $table, $where);

}

So in my index.php, I write this code:

$projectList = new Project;
$projectList->listProjects();

To try and retrieve all of my "projects" stored in the database. I put the "This query senses rows, but returns data it does not" echo in order to determine if rows were being counted, which they are, because that statement is echoed, but my foreach statement is not working correctly. The field in the projects table that I am trying to access is projectName. I would eventually like to display all relevant project info, but I'm sure I could figure that out once I get it to display anything from the database.

develpr
  • 1,296
  • 4
  • 22
  • 42
  • Does your class extend PDO? It should. Also, you're returning `$this` from `action`, which is getting returned from `get`... so you're trying to loop an object in your `foreach` instead of an array result. Try `var_dump()`'ing the `$projects` var after your echo statement. – Rob W Dec 02 '16 at 19:56
  • I'm not sure what you mean by the first part. Part of the purpose of the tutorial was to abstract DB/PDO functions so they can be reused in other pieces of the code or other applications of it. So no, I don't think technically it extends PDO. Is there a way I can loop an object, because in the end that is what I would like to return instead of an array. – develpr Dec 02 '16 at 20:00
  • Try `var_dump` the `$projects` var and see what it is ;) It'll probably be the objects class members/properties versus any actual data. If you extended your class off of PDO, you could potentially do `while($row = $projects->fetch(PDO::FETCH_OBJ)) { /** deal with $row **/ }`, otherwise, you will need to access the underlying PDO object -- such as `$projects->_pdo->fetch()` - assuming it's public. – Rob W Dec 02 '16 at 20:04
  • See http://stackoverflow.com/questions/5175357/extending-pdo-class for example and https://daveismyname.blog/blog/connecting-to-your-mysql-database-with-pdo-php-data-object -- by extending PDO, you can create your own methods, overload PDO's methods, etc... without any hacks. – Rob W Dec 02 '16 at 20:06
  • Sweet okay, and yes, the var_dump on $projects returns no actual "project" data, just a bunch of DB connection data (I think), and for some reason also my user data (because I'm logged in?) I will give that a look and try to figure out how to access the PDO object. – develpr Dec 02 '16 at 20:07

1 Answers1

-2

This:

 foreach($projects as $project) {
            echo $project->projectName;
        }

should be:

 foreach($projects as $project) {
                echo $projects->projectName;
            }
dave
  • 1,410
  • 1
  • 16
  • 42
  • But isn't the foreach loop returning the second variable ($project) as a placeholder for each instance of $projects? – develpr Dec 02 '16 at 20:02
  • Apologies, I was very tired when I looked at this, you are correct. – dave Dec 04 '16 at 10:38