'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