Jump to content

ARGH! Classes!


Jamesking56

Recommended Posts

Hi,Basically I have made a small database class for handling DB connectionsIt connects by itself once declared and sets $currconn as true if it is connected.My other classes Extend from my database class and check if its connected by checking the Parent $currconn.If its not connected it instructs my database class to connect.Now here is my problem.My DB class is declared and connectsMy Settings class (Extending Database) then checks for connection but sees it as FALSE! and tries to get the database to connect with no details?WTF? is going on?Database.class.php:

<?php	####################################################	## PowerCP ~ The Ultimate Radio System	## PowerCP requires a license for use. It is illegal	## to bypass the licensing and encryption systems.	####################################################	## (c) Copyright PowerCP, All Rights Reserved	## www.powercp.co.uk for more information & support	####################################################	## File Name: database.class.php	## Description: The Database Class manages connections	## to many different database systems.	## Category: Class	####################################################	class Database	{		public static $dbtype;		public static $dbconn;		public static $dbuser;		public static $dbpass;		public static $dbname;		public static $currconn;				public function _construct ( $dbtype=null, $dbconn=null, $dbuser=null, $dbpass=null, $dbname=null )		{			if ( !$this->currconn )			{				// No Current Connection!				// Set Vars				$this->dbtype = $dbtype;				$this->dbconn = $dbconn;				$this->dbuser = $dbuser;				$this->dbpass = $dbpass;				$this->dbname = $dbname;								// Connect				$connectit = $this->connect($this->dbtype, $this->dbconn, $this->dbuser, $this->dbpass, $this->dbname);				if ( !$connectit )				{					die("Error: Could not connect to database!");				}				else				{					// Do Nothing!				}			}		}				function connect ( $type, $conn, $user, $pass, $name )		{			// Check type			switch ( $type )			{				case 'mysql':					// Connect to MySQL & Select DB					$tryandconn = @mysql_select_db($this->dbname, @mysql_connect($this->dbconn, $this->dbuser, $this->dbpass));					if ( !$tryandconn )					{						$this->currconn = false;						die("Error! Could not connect to MySQL Database! Please check your settings!");					}					else					{						$this->currconn = true;						return true;					}				break;				default:					// Whoops! Not supported!					die("Oops! That database type is unsupported in this version of PowerCP.");				break;			}		}				function query ( $sql )		{			// Check for Connection			if ( !$this->currconn )			{				// Not connected!				$this->connect($this->dbtype, $this->dbconn, $this->dbuser, $this->dbpass, $this->dbname);			}						// Do Query			if ( !$sql )			{				return false;			}			else			{				// Type Switch				switch ( $this->dbtype )				{					case 'mysql':						$query = @mysql_query(@mysql_real_escape_string($sql));						return $query;					break;				}			}		}				function fetch_array ( $query )		{			// Check for Connection			if ( !$this->currconn )			{				// Not connected!				$this->connect($this->dbtype, $this->dbconn, $this->dbuser, $this->dbpass, $this->dbname);			}			// Do Query			if ( !$sql )			{				return false;			}			else			{				// Type Switch				switch ( $this->dbtype )				{					case 'mysql':						$array = @mysql_fetch_array($query);						return $array;					break;				}			}		}				function fetch_assoc ( $query )		{			// Check for Connection			if ( !$this->currconn )			{				// Not connected!				$this->connect($this->dbtype, $this->dbconn, $this->dbuser, $this->dbpass, $this->dbname);			}			// Do Query			if ( !$sql )			{				return false;			}			else			{				// Type Switch				switch ( $this->dbtype )				{					case 'mysql':						$array = @mysql_fetch_assoc($query);						return $assoc;					break;				}			}		}	}?>

Settings.class.php:

<?php	####################################################	## PowerCP ~ The Ultimate Radio System	## PowerCP requires a license for use. It is illegal	## to bypass the licensing and encryption systems.	####################################################	## (c) Copyright PowerCP, All Rights Reserved	## www.powercp.co.uk for more information & support	####################################################	## File Name: settings.class.php	## Description: The Settings Class uses the Database	## to grab settings.	## Category: Class	####################################################		class Settings extends Database	{		function getsetting ( $setting )		{			if ( !parent::$currconn )			{				parent::connect(parent::$dbtype, parent::$dbconn, parent::$dbuser, parent::$dbpass, parent::$dbname);			}			// Grab the setting			return parent::query("SELECT `value` FROM settings WHERE `key`='".filter($setting)."'");		}				function setsetting ( $key, $newsetting )		{			if ( !parent::$currconn )			{				parent::connect(parent::$dbtype, parent::$dbconn, parent::$dbuser, parent::$dbpass, parent::$dbname);			}			// Set the setting			$insert = parent::query("UPDATE settings SET `value`='".filter($newsetting)."' WHERE `key`='".filter($key)."'");		}	}?>

So my page now says "HERE!Oops! That database type is unsupported in this version of PowerCP.". I used the 'HERE!' to debug it so I could track where its at.Can someone tell me how I can resolve this so it only connects once and all other classes know that it has connected?

Link to comment
Share on other sites

Hi,Basically I have made a small database class for handling DB connectionsIt connects by itself once declared and sets $currconn as true if it is connected.My other classes Extend from my database class and check if its connected by checking the Parent $currconn.Can someone tell me how I can resolve this so it only connects once and all other classes know that it has connected?
I thought this was a singleton db class at first when I saw all the static variables. When you set the currconn property after the connection is made, you're setting it as one of the object's properties ($this->currconn), when you access the curconn property from the child class, you're accessing the static property (parent::$currconn) that you originally defined in the parent class, which was never set by anything. You are essentially looking at two different properties.I imagine this is what you were trying to do for the child class:
	class Settings extends Database	{		function getsetting ( $setting )		{			if ( !$this->currconn )			{				$this->connect($this->dbtype, $this->dbconn, $this->dbuser, $this->dbpass, $this->dbname);			}			// Grab the setting			return $this->query("SELECT `value` FROM settings WHERE `key`='".filter($setting)."'");		}				function setsetting ( $key, $newsetting )		{			if ( !$this->currconn )			{				$this->connect($this->dbtype, $this->dbconn, $this->dbuser, $this->dbpass, $this->dbname);			}			// Set the setting			$insert = $this->query("UPDATE settings SET `value`='".filter($newsetting)."' WHERE `key`='".filter($key)."'");		}	}?>

The variables in the parent class should be defined as:

		public $dbtype;		public $dbconn;		public $dbuser;		public $dbpass;		public $dbname;		public $currconn;

You should read up to better understand the difference between static class properties/methods and object properties. http://us2.php.net/manual/en/language.oop5.php

Link to comment
Share on other sites

After changing to public and updating my settings class back to using $this I get the error:"Oops! That database type is unsupported in this version of PowerCP."How come I still get the same error?

Link to comment
Share on other sites

After changing to public and updating my settings class back to using $this I get the error:"Oops! That database type is unsupported in this version of PowerCP."How come I still get the same error?
I'm guessing it means your server doesn't have mysql installed on it.
Link to comment
Share on other sites

I'm still getting the same issue, My Server does have MySQL and it can connect but when my extended class detects if its connected or not it thinks it isn't and tries again to connect...Its something to do with how my variables are stored...I need my variables shared between all extended classes!How can I do this?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...