'ello all.

I have this class, I dont think it is done very well.

First of all, it doesn't insert data or barf like it should when it doesn't insert the data.

Could someone tell me why, and possibly let me in on how this class could be written better?

Thanks!

<?php
  class SD_Player {

protected $db;

protected $id;
protected $name;
protected $cash;
protected $life;
protected $location;
protected $days;

public function __construct(DB_common $db, $id){
  $this->db = $db;
  $this->id = (int)$id;
  try {
    if($this->hasGame() == 0)
      try {
        $this->createGame();
      } catch (Exception $e) {
        die($e->getMessage());
      }
  } catch (Exception $e) {
    die($e->getMessage());
  }
  try {
    $this->loadPlayer();
  } catch (Exception $e) {
    die($e->getMessage());
  }
}

protected function hasGame(){
  $sql = "SELECT COUNT(player) FROM sd_games WHERE player='".$this->id."'";
  $query = $this->db->query($sql);
  if(PEAR::isError($query))
    throw new Exception("Exception thrown at line ".__LINE__." in class ".getclass($this));
  else
    return (int)$query->numRows();
}

protected function createGame(){
  $sql = "INSERT INTO sd_games (player, days, cash, location, life) VALUES ('".$this->id."', 0, 100, 1, 100)";
  $query = $this->db->query($sql);
  if($query) {}
  if(PEAR::isError($query))
    throw new Exception("Exception thrown at line ".__LINE__." in class ".getclass($this));

}

protected function loadPlayer(){
  $data = $this->db->getRow("SELECT * FROM sd_games WHERE player='".$this->id."' LIMIT 1", array(), DB_FETCHMODE_ASSOC);
  if(PEAR::isError($data))
    throw new Exception("Exception thrown at line ".__LINE__." in class ".getclass($this));
}

  }
?>

edit
I am taking the advice of brad on the boards and left out the id auto_increment field at the beginning of the SQL "INSERT INTO sd_games (player, days, cash, location, life) VALUES ('".$this->id."', 0, 100, 1, 100)"
Just as an FYI
/edit

    15 days later

    Do you not get an error? If so, what is the error.

      Do you have errors and notice display on? If not or you are unsure insert the following line at the top of the script:

      error_reporting(E_ALL);
      

      Also, could you post the code that uses the class and the createGame function?

        Error reporting is on.

        The createGame method is in the above class.

        I create an instance like so.

        try{
          $player = new Player($db, 1);
        } catch (Exception $e){
         die($e->getMessage());
        }

          Have a look at your hasGame method:

              protected function hasGame(){
                $sql = "SELECT COUNT(player) FROM sd_games WHERE player='".$this->id."'";
                $query = $this->db->query($sql);
                if(PEAR::isError($query))
                  throw new Exception("Exception thrown at line ".__LINE__." in class ".getclass($this));
                else
                  return (int)$query->numRows();
              }
          

          The number of rows returned from the SQL query is always going to be one row. This row in turn will contain a single column telling you how many games the player has. Your hasGame method always returns 1 and for this reason the createGame method is never executed.

          Change the return of the hasGame method to:

          $query->fetchInto($row)
          
          return $row[0];
          

          You hasGame method should really return a boolean value because it does not (as indicated by its name) tell you weather or not a user has a game, it tells you how many games the user has.

          It is also advisable to enclose ALL if statements in curly brackets, even single line statements as this aids readability and reduces the scope for parse errors that result from nesting with the incorrect number of curly braces 😉.

            In order to successfully debug a parse error, you need to ensure that error reporting is set to E_ALL, and that errors are either displayed, logged or both.

            Once you've done that, you will see the error message on the error log.

            Until then, we can't help you.

            After then, you can probably help yourself.

            Mark

              His code will parse fine. Its the logic that's causing the error.

                visualAd wrote:

                His code will parse fine. Its the logic that's causing the error.

                Exactly.

                Merci merci!

                  Write a Reply...