UnknownPlayer Posted July 13, 2011 Share Posted July 13, 2011 How can i use this class to get users data but i need to call it like this: $user_data = new get_user_data(I THOUGHT THAT I NEED TO PUT USER ID HERE.. BUT IM WRONG); $var1 = $user_data->get_id(); $var2 = $user_data->get_un(); These is class code: class get_user_data { $query = "SELECT * FROM users WHERE id = {I DONT KNOW HOW TO DO THIS} LIMIT 1"; $result = mysql_query($query); $var = mysql_fetch_object($result); function get_id(){ return $var->ID; } function get_un(){ return $var->username; } } Quote Link to comment https://forums.phpfreaks.com/topic/241898-mysql-in-class/ Share on other sites More sharing options...
premiso Posted July 13, 2011 Share Posted July 13, 2011 What isn't wrong? You really need to read up on the class syntax. // Usage: $userData = get_user_data(1); echo $userData->get_un(); class get_user_data { private $_var; public __construct($id) { $id = (int)$id; $query = "SELECT * FROM users WHERE id = {$id} LIMIT 1"; $result = mysql_query($query) or trigger_error('Query Failed: ' . mysql_error()); $this->_var = mysql_fetch_object($result); } public function get_id(){ return $this->_var->id; } public function get_un(){ return $this->var->username; } } Should get you started. Quote Link to comment https://forums.phpfreaks.com/topic/241898-mysql-in-class/#findComment-1242239 Share on other sites More sharing options...
AbraCadaver Posted July 13, 2011 Share Posted July 13, 2011 What isn't wrong? Quote Link to comment https://forums.phpfreaks.com/topic/241898-mysql-in-class/#findComment-1242258 Share on other sites More sharing options...
UnknownPlayer Posted July 13, 2011 Author Share Posted July 13, 2011 Got this error: Fatal error: Call to undefined function get_user_data() in C:\wamp\www\ttt.php on line 12 but i have class declared like this: class get_user_data { ... } What is problem? Quote Link to comment https://forums.phpfreaks.com/topic/241898-mysql-in-class/#findComment-1242316 Share on other sites More sharing options...
premiso Posted July 13, 2011 Share Posted July 13, 2011 My usage was messed up: $userData = new get_user_data(1); Also in the get_un function, you need to change $this->var to be $this->_var Quote Link to comment https://forums.phpfreaks.com/topic/241898-mysql-in-class/#findComment-1242317 Share on other sites More sharing options...
UnknownPlayer Posted July 13, 2011 Author Share Posted July 13, 2011 Fixed, and this too: public __construct($id) to public function __construct($id).. Thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/241898-mysql-in-class/#findComment-1242318 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.