I am wanting to learn the singleton method, and I am having an issue with my code. I've been manipulating my own code for a few days now, but decided to use someone else's class to give it a try, and I am still coming up with nothing. Any help would be greatly appreciated. The first class below is the database class and the second class is a user class. Basically, I am trying to figure out what I am doing wrong. I am not getting any database errors, so when I try to echo $user->get_my_avatar(); I get nothing.
class chmdb {
/**
* The chmdb database object
*
* @access private
* @var object
*/
private $chmdb;
/**
* MySQLi database object
*
* @access private
* @var object
*/
private static $instance;
/**
* Current result set
*
* @access private
* @var object
*/
private $result;
/**
* The last result (processed)
*
* @access private
* @var array
*/
private $last_result;
/**
* The number of rows from last result
*
* @access private
* @var int
*/
private $row_count;
/**
* Last error
*
* @access private
* @var string
*/
private $last_error;
/**
* PHP5 Constructor
*
* Making this function 'private' blocks this class from being directly created.
*
* @access private
*/
private function __construct() { }
/**
* Creates and references the chmdb object.
*
* @access public
* @return object MySQLi database object
*/
public static function instance() {
if ( !self::$instance )
self::$instance = new chmdb();
return self::$instance;
}
/**
* Connect to the MySQL database.
*
* @param string $dbHost MySQL hostname
* @param string $dbUser MySQL username
* @param string $dbPass MySQL password
* @param string $dbName MySQL database name
* @return bool True if successful, false on error.
*/
public function connect($dbHost, $dbUser, $dbPass, $dbName) {
// Connect to the database
$this->chmdb = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
// Check connection
if ( mysqli_connect_errno() ) {
$this->last_error = mysqli_connect_error();
return false;
}
return true;
}
/**
* Checks for errors.
*
* @return string|false $last_error if it exists or false if no errors.
*/
public function is_error() {
if ( isset($this->last_error) && !empty($this->last_error) )
return $this->last_error;
return false;
}
/**
* Close active connection to MySQL database.
*
* @access public
* @return bool Always returns true.
*/
public function close() {
if ( $this->chmdb )
$this->chmdb->close();
return true;
}
/**
* Executes query and returns results.
*
* @access public
* @param string $sql The SQL statement to execute.
* @return mixed
*/
public function query($sql) {
$this->result = $this->chmdb->query($sql);
return $this->result;
}
public function get_results($sql) {
if ( !$this->query($sql) )
return false;
$num_rows = 0;
while ( $row = $this->result->fetch_object() ) {
$this->last_result[$num_rows] = $row;
$num_rows++;
}
$this->result->close();
return $this->last_result;
}
public function num_rows() {
return (int) $this->row_count;
}
/**
* Retrieve a single row from the database.
*
* Do not include LIMIT 1 on the end, as this will be taken care
* of automatically.
*
* @param string $sql The SQL statement to execute.
* @return object The MySQL row object
*/
public function get_row($sql) {
if ( !$results = $this->query($sql . " LIMIT 1") )
return false;
return $results->fetch_object();
}
/**
* Sanitizes data for safe execution in SQL query.
*
* @access public
* @param mixed $data The data to be escaped.
* @return mixed
*/
public function escape($data) {
return $this->chmdb->real_escape_string($data);
}
/**
* Prevent cloning of chmdb.
*
* @access public
* @return void
*/
public function __clone() {
// Issue E_USER_ERROR if clone is attempted
trigger_error('Cloning <em>chmdb</em> is prohibited.', E_USER_ERROR);
}
/**
* Destructor
*
* @access public
*/
public function __destruct() {}
}
class Users {
public function get_my_avatar() {
$chmdb = chmdb::instance();
$chmdb->connect($dbHost, $dbUser, $dbPass, $dbName);
$result = $chmdb->query("SELECT email, username FROM chm_members WHERE username = 'administrator'");
while($r = $result->fetch_array) {
if (defined('UPLOAD_AVATAR')) {
$filename = CHMEMBER_DIR . 'profile/avatars/'.$r->username.'.gif';
if(file_exists($filename)) {
$mypicture = getimagesize(CHMEMBER_URI . '/profile/avatars/' . $r->username . '.gif');
return '<img src="'.CHMEMBER_URI.'/profile/avatars/'.$r->username.'.gif" '.imgResize($mypicture[1], $mypicture[1], 55). ' alt="'.$r->username.'"/>';
} else {
$mypicture = getimagesize("http://www.gravatar.com/avatar.php?gravatar_id=".md5($r->email));
return '<img src="http://www.gravatar.com/avatar.php?gravatar_id='.md5($r->email).'" '.imgResize($mypicture[1], $mypicture[1], 55). ' alt="'.$r->username.'"/>';
}
} else {
$mypicture = getimagesize("http://www.gravatar.com/avatar.php?gravatar_id=".md5($r->email));
return '<img src="http://www.gravatar.com/avatar.php?gravatar_id='.md5($r->email).'" '.imgResize($mypicture[1], $mypicture[1], 55). ' alt="'.$r->username.'"/>';
}
}
}
}
$user = new Users();