php - Insert User ID in a Session -


i'm trying user id of current user can put in session other use. tested query , returns correct values, when try put these values in "user" object following error!

undefined property: stdclass::$name in c:\xampp\htdocs\project\classes\user.class.php on line 88 

line 88 is:

$this->name = $obj->name; 

<?php  include_once("classes/db.class.php");  class user {     private $m_sname;     private $m_spassword;     private $m_semail;     private $m_sid;      public function __get($p_sproperty)     {         switch($p_sproperty)         {             case "name":             return $this->m_sname;             break;              case "password":             return $this->m_spassword;             break;              case "email":             return $this->m_semail;             break;              case "user_id":             return $this->m_sid;              default:             throw new exception("cannot " . $p_sproperty);         }     }      public function __set($p_sproperty, $p_vvalue)     {            switch($p_sproperty)         {             case "name":             $this->m_sname = mysql_real_escape_string($p_vvalue);             break;              case "password":                  $salt = "kjfiqjifmqjfemq";                 $this->m_spassword = md5($p_vvalue.$salt);              break;              case "email":             $this->m_semail = mysql_real_escape_string($p_vvalue);             break;              case "user_id":             $this->m_sid = $p_vvalue;             break;              default:             throw new exception("cannot set " . $p_sproperty);         }     }      public function register()     {         try         {             $db = new db('localhost', 'root', '', 'project');             $db->insert("user_tbl (username, password, email)", "'$this->name','$this->password', '$this->email'");          }         catch(exception $e)         {             echo $e->message;         }     }      public function canlogin()     {          $db = new db('localhost', 'root', '', 'project');          $res = $db->select("username, id" , "user_tbl","password = '$this->password' , email = '$this->email'");          if($res->num_rows == 1)         {             $obj = $res->fetch_object();             $this->name = $obj->name;             $this->id = $obj->id;             return true;          }else         {             throw new exception('fout bij het aanmelden.');             return false;         }      }  }     ?> 

totally agree @michaelberkowski in comment above. trying refer fields, not loading.

either change code $this->name = $obj->username; or add 'name' query

$res = $db->select("username, id, name" , "user_tbl","password = '$this->password' , email = '$this->email'"); 

as id

you refering wrong variable name. in setter / getter, property name case "user_id":, trying refer via $this->id. need change case option case "id": work.


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -