Problem extending mysqli - "No database connected"

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I have a class that extends msyqli that AFAIK can connect to the
database, but gives me an error when I try and query it. Here's my
class:

-- BEGIN DATABASE CLASS --
require_once('Configuration.php');
require_once('RSFSException.php');

class Database extends mysqli {
	
	private $strUrl = "DEV";
	private $strUser;
	private $strPass;
	private $strHost;
	private $intPort;
	private $strName;
	
	private static $INSTANCE;
	private static $CONFIG;
	
	private function __construct() {
		$this->CONFIG = Configuration::getInstance();
		$this->establishConnectionSettings();
		$this->connect($this->getHost(), $this->getUser(),
			$this->getPass(), $this->getName);

		if (mysqli_connect_errno()) {
           		throw new RSFSException("Connect exception:\n ".
           		mysqli_connect_error(), -1);
       	}
	}

	public static function getInstance($url = null) {
		// Set the URL if one is provided.
		if (isset($url)) {
			$this->setUrl($url);
		}
		
		// Instantiate the Singleton if not already instantiated.
		if(empty(self::$INSTANCE)) {
			self::$INSTANCE = new Database();
		}

		return self::$INSTANCE;
	}
	
	public function establishConnectionSettings() {
		// DEVELOPMENT (default) database settings.
		if ($this->getUrl() == "DEV") {
			$this->setUser($this->CONFIG->getProperty("rsfsTestDbUser"));
			$this->setPass($this->CONFIG->getProperty("rsfsTestDbPass"));
			$this->setHost($this->CONFIG->getProperty("rsfsTestDbHost"));
			$this->setPort($this->CONFIG->getProperty("rsfsTestDbPort"));
			$this->setName($this->CONFIG->getProperty("rsfsTestDbName"));
		}
		
		// PRODUCTION database settings.
		else if ($this->getUrl() == "PROD") {
			$this->setUser($this->CONFIG->getProperty("rsfsDbUser"));
			$this->setPass($this->CONFIG->getProperty("rsfsDbPass"));
			$this->setHost($this->CONFIG->getProperty("rsfsDbHost"));
			$this->setPort($this->CONFIG->getProperty("rsfsDbPort"));
			$this->setName($this->CONFIG->getProperty("rsfsDbName"));			
		}
		
		else {
			// Throw an exception.		
		}
	}
	
	public function query($sql) {
		$result = parent::query($sql);
		
		if(mysqli_error($this)){
     		throw new RSFSException(mysqli_error($this), mysqli_errno($this));
		}
   	
   	return $result;
	}
		
	public function valueExists($table, $column, $value,
		$caseInsensitive = false) {
		if (!$caseInsensitive) {
			$sql = "SELECT * FROM $table WHERE $column = $value";
		}
		
		else {
			$sql = "SELECT * FROM $table " .
				"WHERE upper($column) = ($value)";
		}

		$result = $this->query($sql);
		echo "Result: $result\n";
		$count = $result->num_rows;

		if ($count > 0) {
			return true;
		}
		
		else {
			return false;
		}
	}
	
	public function valuesExist($table, $columns, $values) {
		// Placeholder.
	}
	
	public function procedureQuery($name, $params) {
		return $this->query("call $name($params)");
	}
	
	/** Return the URL. */
	public function getUrl() {
		return $this->strUrl;
	}
	
	.
	.
	.
	
	/** Set the database URL. */
	public function setName($url) {
		$this->strUrl = $$url;
	}
}

-- END DATABASE CLASS --

To test it, I'm doing the following:

-- BEGIN --
echo "Attempting to establish connection.\n";
$CONN = Database::getInstance();

if ($CONN->ping()) {
   printf ("Our connection is ok!\n");
} else {
   printf ("Danger, Will Robinson!!!\n");
}

echo "Attempting to test query() method.\n";
if ($result = $CONN->query("SELECT * FROM country")) {
	echo "In result set\n";
	while( $row = mysqli_fetch_assoc($result) ){
		printf("%s (%s)\n", $row['cnt_code'], $row['cnt_name']);
	}
}
-- END --

Which outputs the following:

Attempting to establish connection.
localhost:rsfs:rsfs:rsfs
Our connection is ok!
Attempting to test query() method.


Fatal error:  Uncaught RSFSException: [1046]: No database selected

 thrown in /home/lee/Development/rsfs/php/classes/core/Database.php on line 84

I've tried removing the query method from my class, but even Mysqli's
query() isn't working. Can anyone help me identify the problem?

Thanks!
  Lee

PS: I posted a similar question a few weeks ago - I though I had it
working when I saw that it was connecting. Now that I'm ready to
actually use it, I see that it just can't query...

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux