Clandestinex337 Posted June 26, 2011 Share Posted June 26, 2011 So my other functions work, where I create new user and update user, but showing the users doesn't seem to work class MySql { var $host; var $user; var $password; var $database; public $db_handle; public function connect($db_host, $db_user, $db_password, $db_database) { $this->host = $db_host; $this->user = $db_user; $this->password = $db_password; $this->database = $db_database; $this->db_handle = mysql_connect($this->host, $this->user, $this->password)or die('could not connect to db'); mysql_select_db($this->database)or die('could not select db'); } public function query($sql) { return mysql_query($sql, $this->db_handle) or die (mysql_error()); } public function fetch($sql) { return mysql_fetch_assoc($this->query($sql), $this->db_handle); } } class User { public $db; public $id; public $username; public $password; public function __construct(MySql $db, $id = '0') { $this->db = $db; $this->id = $id; if($this->$id !== 0) { $result = $this->db->query("SELECT `username`, `password` FROM `user` WHERE `id` ='{$this->id}'"); if($result) { $this->username = $result->username; $this->password = $result->password; } } } $user = new User($connect, 31); echo $user->getUsername(); I can't seem to spot why its not working This is my print_r User Object ( [db] => MySql Object ( [host] => localhost [user] => root [password] => [database] => imgnexus [db_handle] => Resource id #4 ) [id] => 31 [username] => [password] => ) let me know if you need the full code.. Cheers Quote Link to comment Share on other sites More sharing options...
Clandestinex337 Posted June 26, 2011 Author Share Posted June 26, 2011 I believe I have to implement my fetch($sql) into the _construct, but i am not sure where it is supposed to fit Quote Link to comment Share on other sites More sharing options...
WebStyles Posted June 26, 2011 Share Posted June 26, 2011 I'm thinking that your code sequence... $id = '0' ... $this->id = $id; ... if($this->$id !== 0) this will prevent it from executing. also, if `id` in your database is a key/autoincrement 0 is not a valid number. Quote Link to comment 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.