jeeva Posted March 10, 2007 Share Posted March 10, 2007 his frnds, i am not good in php class,so i need ur help .. i have one class as database wrapper and i need to use this class from another class.i hear that i need to create one object to call that class.but i have used like that but its not working i dont know whether its right or wrong. Here my code class db { var $dsn="dsn"; //dsn var $uname=""; var $password=""; var $con=NULL; //db connect function DbConnect() { $this->con=odbc_connect($this->dsn,$this->uname,$this->password) or die(odbc_errormsg()); return $this->con; } //function to select function Select($tableName,$condition) { $sql="SELECT * from $tableName WHERE 1=1 $condition"; $this->select=odbc_exec($this->con,$sql); return $this->select; } //get the values from result function Getdata($resourceid,$fieldName) { $result=odbc_result($resourceid,$fieldName); return $result; } } class GlobalVars { $db=new db(); $dbcon=$db->DbConnect(); function UserDetails($fieldName) { $tableName="Employee"; $condition="and Windowloginname='$loginuser'"; $select=$this->db->Select($tableName,$condition); $value=$this->db->GetData($select,fieldName); return $value; } } can u please tell me wt i have to do? jeeva Quote Link to comment https://forums.phpfreaks.com/topic/42080-using-class-in-php/ Share on other sites More sharing options...
per1os Posted March 10, 2007 Share Posted March 10, 2007 Constructors are key here. They are what get called when the class is instantiated. Although this is a poorly designed class here is what it should be. <?php class db { var $dsn="dsn"; //dsn var $uname=""; var $password=""; var $con=NULL; //db connect // constructor function db() { $this->dsn="dsn"; //dsn -- for PHP 4 as the variable declaration does not work. $this->DbConnect(); } function DbConnect() { $this->con=odbc_connect($this->dsn,$this->uname,$this->password) or die(odbc_errormsg()); return $this->con; } //function to select function Select($tableName,$condition) { $sql="SELECT * from $tableName WHERE 1=1 $condition"; $this->select=odbc_exec($this->con,$sql); return $this->select; } } class GlobalVars { // constructor function GlobalVars() { $db=new db(); } function UserDetails($fieldName) { $tableName="Employee"; $condition="and Windowloginname='$loginuser'"; $select=$this->db->Select($tableName,$condition); $value=$this->db->GetData($select,fieldName); return $value; } //get the values from result function Getdata($resourceid,$fieldName) { $result=odbc_result($resourceid,$fieldName); return $result; } } ?> If you would like to take a look at my db class just let me know I will be more than happy to show it off. --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42080-using-class-in-php/#findComment-204108 Share on other sites More sharing options...
jeeva Posted March 10, 2007 Author Share Posted March 10, 2007 Please show me that..that will enable me to learn........ Quote Link to comment https://forums.phpfreaks.com/topic/42080-using-class-in-php/#findComment-204111 Share on other sites More sharing options...
per1os Posted March 10, 2007 Share Posted March 10, 2007 http://www.aeonity.com/files/db.class.phps --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42080-using-class-in-php/#findComment-204115 Share on other sites More sharing options...
jeeva Posted March 10, 2007 Author Share Posted March 10, 2007 thank u FrosT... but in my code still giving problem like Fatal error: Call to a member function on a non-object in c:\inetpub\wwwroot\conference\include\globals_Conference.php Quote Link to comment https://forums.phpfreaks.com/topic/42080-using-class-in-php/#findComment-204122 Share on other sites More sharing options...
per1os Posted March 10, 2007 Share Posted March 10, 2007 changed: $this->db=new db(); my bad. Forgot about that portion. <?php class db { var $dsn="dsn"; //dsn var $uname=""; var $password=""; var $con=NULL; //db connect // constructor function db() { $this->dsn="dsn"; //dsn -- for PHP 4 as the variable declaration does not work. $this->DbConnect(); } function DbConnect() { $this->con=odbc_connect($this->dsn,$this->uname,$this->password) or die(odbc_errormsg()); return $this->con; } //function to select function Select($tableName,$condition) { $sql="SELECT * from $tableName WHERE 1=1 $condition"; $this->select=odbc_exec($this->con,$sql); return $this->select; } } class GlobalVars { // constructor function GlobalVars() { $this->db=new db(); } function UserDetails($fieldName) { $tableName="Employee"; $condition="and Windowloginname='$loginuser'"; $select=$this->db->Select($tableName,$condition); $value=$this->db->GetData($select,fieldName); return $value; } //get the values from result function Getdata($resourceid,$fieldName) { $result=odbc_result($resourceid,$fieldName); return $result; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/42080-using-class-in-php/#findComment-204123 Share on other sites More sharing options...
jeeva Posted March 10, 2007 Author Share Posted March 10, 2007 now its working... thanks a lot frost........ Quote Link to comment https://forums.phpfreaks.com/topic/42080-using-class-in-php/#findComment-204134 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.