jiros1 Posted September 28, 2015 Share Posted September 28, 2015 (edited) Greetings, thank you for taking the time and reading my question. I'm trying to use PDO for the first time within a object and I'm trying to iterate through my array and displaying the right record (Game in this case). This is the error I get: Fatal error: Call to a member function getAll() on a non-object in /media/sf_sandbox/boardgame-collection/view/manage-games.php on line 34 And this is what I want: Descent second edition World of Warcraft the boardgame Here is the code of my model: The Game model class Game { public $ID = ""; public $Name =""; public function getAll(){ require "include/dbconnect.php"; $sql = "SELECT * FROM TBL_Game"; $query = $db->prepare($sql); $query->execute(); $result = $query->fetchAll(); return $result; } } Now I'm trying to get it by doing this: manage-games.php <?php require "model/Game.php"; $Game = new Game; $Game = $Game->getAll(); while($Game = $Game->getAll()){ echo $Game->Name; } ?> Edited September 28, 2015 by jiros1 Quote Link to comment https://forums.phpfreaks.com/topic/298338-iterate-through-pdo-select-method-from-object/ Share on other sites More sharing options...
Solution Ch0cu3r Posted September 28, 2015 Solution Share Posted September 28, 2015 Here in manage-games.php $Game = $Game->getAll(); while($Game = $Game->getAll()){ You are calling $Game->getAll(), this is calling your getAll method which returns array of results from the query. You are then assigning what is returned by the method to $Game, this will cause $Game to no longer store an instance of the Game object. You then go to call $Game->getAll() in the while the loop. This is where the error is caused because $Game is no longer the Game object, it is only the array of results. What you want to do is this // get array of results from the query $result = $Game->getAll(); // loop over array of results using foreach loop foreach($result as $row) { echo $row['name'] . '<br />'; } When using the databases in the game model you should not use require "include/dbconnect.php"; to use the database. Instead what you should do is pass the database instance when initializing the Game object // connect to database outside the class require "include/dbconnect.php"; // pass the database connection/instance to the Game object on initialization $Game = new Game($db); In the Game class save the database instance as a property in the constructor class Game { private $db; function __construct(PDO $db) { // set database property $this->db = $db; } ... rest of class here ... } Now in the getAll() method $db will need to be changed to $this->db Quote Link to comment https://forums.phpfreaks.com/topic/298338-iterate-through-pdo-select-method-from-object/#findComment-1521680 Share on other sites More sharing options...
jiros1 Posted September 28, 2015 Author Share Posted September 28, 2015 Hi Ch0cu3r, It works now and thank you for going through the steps of explaining to me what I was doing wrong, it's nice to get my code working but even better to learn from it. In the second part you handed me some material to think about improving my database connections, It looks great the way you do it, but it's not working for me. My guess is that I'm doing something wrong here: It gives me the following errors: Warning: Missing argument 1 for Game::__construct()Notice: Undefined variable: db in manage-games.phpNotice: Undefined variable: db in Game.phpFatal error: Call to a member function prepare() on a non-object in Game.php My current updated Game.php model: <?php require "include/dbconnect.php"; class Game { public $ID = ""; public $Name =""; private $db; function __construct($db){ $this->db = $db; } public function getAll(){ $this->db; //require "include/dbconnect.php"; $sql = "SELECT * FROM TBL_Game"; $query = $db->prepare($sql); $query->execute(); $result = $query->fetchAll(); return $result; } } My current updated manage-games.php view: <?php $Game = new Game; $Game = new Game($db); $result = $Game->getAll(); foreach($result as $row){ echo $row['Name'] . '<br />'; } ?> Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/298338-iterate-through-pdo-select-method-from-object/#findComment-1521783 Share on other sites More sharing options...
iarp Posted September 28, 2015 Share Posted September 28, 2015 (edited) You need to re-read Ch0cu3r's post. It answers all of the issues you're having. You're calling new Game twice in manage-games.php. You need to fix $db into $this->db Edited September 28, 2015 by iarp Quote Link to comment https://forums.phpfreaks.com/topic/298338-iterate-through-pdo-select-method-from-object/#findComment-1521791 Share on other sites More sharing options...
hansford Posted September 29, 2015 Share Posted September 29, 2015 (edited) So, based on what Ch0cu3r stated, your function should look like this. public function getAll(){ $sql = "SELECT * FROM TBL_Game"; $query = $this->db->prepare($sql); $query->execute(); $result = $query->fetchAll(); return $result; } Edited September 29, 2015 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/298338-iterate-through-pdo-select-method-from-object/#findComment-1521812 Share on other sites More sharing options...
hansford Posted September 29, 2015 Share Posted September 29, 2015 (edited) Here is all of what Ch0cu3r stated. // get array of results from the query $result = $Game->getAll(); // loop over array of results using foreach loop foreach($result as $row) { echo $row['name'] . '<br />'; } class Game { public $ID = ""; public $Name =""; public function getAll(){ $sql = "SELECT * FROM TBL_Game"; $query = $this->db->prepare($sql); $query->execute(); return $query->fetchAll(); } } Edited September 29, 2015 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/298338-iterate-through-pdo-select-method-from-object/#findComment-1521814 Share on other sites More sharing options...
jiros1 Posted October 1, 2015 Author Share Posted October 1, 2015 Thanks guys, it works now, and more importantly, I know why! Thank you for the elaborate explanations! Quote Link to comment https://forums.phpfreaks.com/topic/298338-iterate-through-pdo-select-method-from-object/#findComment-1522086 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.