Re: [PHP] Problem extending mysqli - "No database connected"
- From: ceo@xxxxxxxxx ("Richard Lynch")
- Date: Tue, 3 Jul 2007 01:00:29 -0500 (CDT)
Somewhere in that mess there should be something to do a
mysqli_select_db() to choose WHICH database to use...
Where it's supposed to be in the mess of OOP layers you're wrapped
around yourself, god only knows.
On Fri, June 29, 2007 12:31 pm, Lee PHP wrote:
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
--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
.
- Prev by Date: Re: [PHP] Flash / Ajax / PHP
- Next by Date: Re: Header function breaks on binary files with spaces?
- Previous by thread: Removing Spaces from Array Values
- Next by thread: Re: [PHP] shuffle or mt_rand
- Index(es):
Relevant Pages
|