Calling mysql methods from an extended 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

Post Reply
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Calling mysql methods from an extended class

Post 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,
Last edited by Benjamin on Mon Oct 18, 2010 12:12 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Calling mysql methods from an extended class

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Calling mysql methods from an extended class

Post by Benjamin »

Fixed :drunk:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Calling mysql methods from an extended class

Post by requinix »

Are you using PHP 4?
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Re: Calling mysql methods from an extended class

Post by pinehead18 »

php5
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Re: Calling mysql methods from an extended class

Post by pinehead18 »

bump≥
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Calling mysql methods from an extended class

Post 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).
Post Reply