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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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