Passing SQL Object into new class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

pinehead
Forum Newbie
Posts: 14
Joined: Sat Oct 13, 2012 11:51 am

Passing SQL Object into new class

Post 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,
Last edited by Benjamin on Sun Nov 04, 2012 9:20 pm, edited 1 time in total.
Reason: Added [syntax=php||htm||css||javascript||sql||etc] - Please use [syntax] tags when posting code in the forums! Thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Passing SQL Object into new class

Post by requinix »

That would be it.
pinehead
Forum Newbie
Posts: 14
Joined: Sat Oct 13, 2012 11:51 am

Re: Passing SQL Object into new class

Post 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,
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Passing SQL Object into new class

Post by requinix »

I don't see you calling query() in that code...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Passing SQL Object into new class

Post 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();
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Passing SQL Object into new class

Post by Mordred »

Surely you mean $this->db?
pinehead
Forum Newbie
Posts: 14
Joined: Sat Oct 13, 2012 11:51 am

Re: Passing SQL Object into new class

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Passing SQL Object into new class

Post by requinix »

Your constructor lost an underscore along the way.
pinehead
Forum Newbie
Posts: 14
Joined: Sat Oct 13, 2012 11:51 am

Re: Passing SQL Object into new class

Post 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,
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Passing SQL Object into new class

Post by Mordred »

Show us how you construct the database and the Achievements objects
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Passing SQL Object into new class

Post 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().
(#10850)
pinehead
Forum Newbie
Posts: 14
Joined: Sat Oct 13, 2012 11:51 am

Re: Passing SQL Object into new class

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

	
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Passing SQL Object into new class

Post by Christopher »

Which means the problem is this:

Code: Select all

$achievement = new Achievement($db);    // $db is not an object with at query() method
(#10850)
pinehead
Forum Newbie
Posts: 14
Joined: Sat Oct 13, 2012 11:51 am

Re: Passing SQL Object into new class

Post 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.
pinehead
Forum Newbie
Posts: 14
Joined: Sat Oct 13, 2012 11:51 am

Re: Passing SQL Object into new class

Post 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
		
		
	}
	
	
}
Post Reply