Jump to content

Iterate through PDO select method from object


jiros1
Go to solution Solved by Ch0cu3r,

Recommended Posts

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 by jiros1
Link to comment
Share on other sites

  • Solution

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

Link to comment
Share on other sites

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.php
Notice: Undefined variable: db in Game.php
Fatal 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!

Link to comment
Share on other sites

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 by hansford
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.