Page 1 of 1

Calling mysql methods from an extended class

Posted: Sun Oct 17, 2010 10:58 pm
by pinehead18
Hey everyone, my mysql() class works just fine, i've been using it in my code. This is my first attempt at OOP and i'm trying to extend the class to perform some other frequent tasks for me but in oop. Guidence would be very much appreciated. Here is the code, and the errors below

Code: Select all

class MySQL {


	var $link_id;
	var $Result;
	var $Record = array();
	var $query;
	
	function __construct() {
	
		$this->link_id = mysqli_connect("localhost", "", "") or $this->Error('Not connecting to the database');
		$this->db = mysqli_select_db($this->link_id,'share') or $this->Error(mysqli_error());
		return true;
	
	}
	
	function Query($sql)
        {
          [b]  $this->Result = mysqli_query($this->link_id,$sql);[/b]
            $this->query = $sql;
           #print $sql;
            return $this->Result;
        } 

	function FetchArray() {
		
	[b]	$this->Record = mysqli_fetch_array($this->Result,MYSQLI_ASSOC);[/b]
		return $this->Record;
	
		}
		
	function FetchRow() {
		$this->Record = mysqli_fetch_row($this->Result) or $this->Error(mysqli_error());
		return $this->Record;
		}
		
	function NumRows() {
		$this->Record = mysqli_num_rows($this->Result);
		return $this->Record;
		
	}
	
}


class share extends MySQL { 

	var $row;


	function __construct() { 
	
		$this->db = new MySQL(); 
	
	}


	function feed($user_id,$returnnum) { 
	
		$this->query("SELECT * FROM feed WHERE user_id = $user_id");
		$this->FetchArray($this->query);
		[b]$this->row($this->FetchArray);[/b]
		$this->message = substr($this->row['message'],0,180);
			return $this->message;
	
	}
	


}



Errors:

Warning: mysqli_query() expects parameter 1 to be mysqli, null given on line 20

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given ion line 28

Fatal error: Call to undefined method share::row() on line 63

The lines to the respective code are bolded in the code.

Thanks,

Re: Calling mysql methods from an extended class

Posted: Mon Oct 18, 2010 12:02 am
by requinix

Code: Select all

 tags don't work properly. You need to use [syntax=php] instead. You also can't use BBCode inside.
How about editing your post and fixing those two issues?

Re: Calling mysql methods from an extended class

Posted: Mon Oct 18, 2010 12:12 am
by Benjamin
Fixed :drunk:

Re: Calling mysql methods from an extended class

Posted: Mon Oct 18, 2010 12:50 am
by requinix
Are you using PHP 4?

Re: Calling mysql methods from an extended class

Posted: Mon Oct 18, 2010 8:45 am
by pinehead18
php5

Re: Calling mysql methods from an extended class

Posted: Wed Oct 20, 2010 10:23 pm
by pinehead18
bump≄

Re: Calling mysql methods from an extended class

Posted: Wed Oct 20, 2010 10:43 pm
by John Cartwright
Since you are overriding your parent __construct() method, you either need to explicitely call that the parent constructor from the child class, i.e.,

Code: Select all

class share extends Mysql
{
   public function __construct() { 
      parent::__construct();
   }
}
.. but in your case you simply want to omit the child's constructor, since there is no need to overload the parent's constructor (you only want to do this when you want to add additional logic in the child class outside the scope of the parent, but in your case the parent is doing all the leg work in the object construction).

Next, since you are using inheritance, you do not need to create a new instance of the mysql class inside your child, since the child class technically already is an instance of mysql (it extends it).

So again, for the sake of making your code work, simply delete the "share" object's constructor (or remove the logic in there, and call the parent constructor from the child's contructor as shown above).