Page 1 of 2

Passing SQL Object into new class

Posted: Sun Nov 04, 2012 7:13 pm
by pinehead
I have an active mysql object that contains my conections etc.. $db->query(my query).
I want to pass this object into a new class so i don't have to create the object again

How would i go about passing it into the new class so it's still active?

Code: Select all

class NewClass {
  var $db;
  function __construct($db) {
   $this->db=$db;
 }

}

Thoughts?

Thanks,

Re: Passing SQL Object into new class

Posted: Sun Nov 04, 2012 9:11 pm
by requinix
That would be it.

Re: Passing SQL Object into new class

Posted: Mon Nov 05, 2012 8:32 pm
by pinehead
Well, i thought but it get the following error:] PHP Fatal error: Call to a member function query() on a non-object in /content/disk/sites/linuxacademy/a/classes/achievements.class.php on line 15

Now here is the deal $db is what I pass into the following class..

Now i know db is my class because it's what i use throughout the code. When i do var_dump($db); it posts the database name

Code: Select all

class Achievements {
	
	var $db;
	
	function _construct($db) {
	   $this->db = $db;
	  
		
	}
	
	}
	
Thoughts?

Thanks,

Re: Passing SQL Object into new class

Posted: Mon Nov 05, 2012 8:40 pm
by requinix
I don't see you calling query() in that code...

Re: Passing SQL Object into new class

Posted: Tue Nov 06, 2012 2:51 am
by Weirdan
You need to pass the db object to class constructor during object instantiation:

Code: Select all

// assuming you have $db object in scope
$achievements = new Achievements($db);
$achievements->getGreatest();

Re: Passing SQL Object into new class

Posted: Tue Nov 06, 2012 4:36 am
by Mordred
Surely you mean $this->db?

Re: Passing SQL Object into new class

Posted: Wed Nov 07, 2012 8:01 pm
by pinehead
Exact code..

Code: Select all

$achievements = new achievements($db);
$achievements->getPointValue("1");



class Achievements {
	
	var $dbase;
	
	function _construct($db) {
	   $this->db = $db;
	   
	}

	function getPointValue($action) {
		$sql = $this->db->query("SELECT * FROM points");
		while($this->db->fetcharray($sql)) {
			echo $row['video'];
			
		}
		
	}
	
	function addPoints($userid, $action) {
		//userid is the user id //action is the event that occured will pull from db
		
		
	}
	
	
}
Errors out with that error above. however as you can see i'm passing it in. If i do a var_dump($db) ABOVE the new class instance $ach = new etc.. It dumps the db name so i know it's set.

Re: Passing SQL Object into new class

Posted: Wed Nov 07, 2012 8:05 pm
by requinix
Your constructor lost an underscore along the way.

Re: Passing SQL Object into new class

Posted: Thu Nov 08, 2012 7:55 pm
by pinehead
Call to a member function query() on a non-object in /content/disk/sites/linuxacademy/a/classes/achievements.class.php on line 23

Added the _ on the constructor but am still receiving this error message. Line 23 is $sql = $db->query("SELECT * FROM points");

query is a method in the mysql object contained in $db that works in the $db variable being passed into the achievements class.

Thoughts?

Thanks,

Re: Passing SQL Object into new class

Posted: Fri Nov 09, 2012 6:01 am
by Mordred
Show us how you construct the database and the Achievements objects

Re: Passing SQL Object into new class

Posted: Fri Nov 09, 2012 3:30 pm
by Christopher
pinehead wrote:Added the _ on the constructor but am still receiving this error message. Line 23 is $sql = $db->query("SELECT * FROM points");
According to the error message, you did not change the code to $this->db->query().

Re: Passing SQL Object into new class

Posted: Fri Nov 09, 2012 11:18 pm
by pinehead
Again, here is the exact code with the error message. Errors out on $sql = $this->db->query("SELECT * FROM points");
error : Call to a member function query() on a non-object

Code: Select all


class Achievements {
	
	var $db;
	
	function __construct($db) {
	   $this->db = $db;
	   
	   
	}

	public function achievement($action) {

		$sql = $this->db->query("SELECT * FROM points");
		echo "did something";
		while($this->db->fetcharray($sql)) {
			
			echo "did something";
		}
		return $action;
		
	}
	

	
}

Re: Passing SQL Object into new class

Posted: Sat Nov 10, 2012 11:35 am
by Christopher
Which means the problem is this:

Code: Select all

$achievement = new Achievement($db);    // $db is not an object with at query() method

Re: Passing SQL Object into new class

Posted: Sat Nov 10, 2012 5:04 pm
by pinehead
It is an object with a query() method. In fact in a line of code just above the new achievement instance i use $db->query("select name from users"); and it works just fine.

Re: Passing SQL Object into new class

Posted: Sat Nov 10, 2012 5:49 pm
by pinehead
So i know it has a method 1.) because i use it throughout the other 400 lines of code i ahve 2.) here is a new instance of the db class "inside" the achievements class

Code: Select all

class Achievements {
	
	var $db;
	
	function __construct($host,$user,$password,$db) {
	   $this->db = new MySQL($host,$user,$password,$db);
	   
	   
	}

	public function achievement($action) {

		$sql = $this->db->query("SELECT * FROM points");
		echo "did something";
		while($this->db->fetcharray($sql)) {
			
		}
		
		return $host;
		
	}
	
	function addPoints($userid, $action) {
		//userid is the user id //action is the event that occured will pull from db
		
		
	}
	
	
}