Jump to content

mysql in class


UnknownPlayer

Recommended Posts

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;
		}
	}

Link to comment
https://forums.phpfreaks.com/topic/241898-mysql-in-class/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/241898-mysql-in-class/#findComment-1242239
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.