To my eye my what I typed looks exactly the same but I’ve must of messed up the syntax somewhere.
Tut’s Code
[php]<?php
class WishDB extends mysqli {
// single instance of self shared among all instances
private static $instance = null;
// db connection config vars
private $user = "phpuser";
private $pass = "phpuserpw";
private $dbName = "wishlist";
private $dbHost = "localhost";
private $con = null;
//This method must be static, and must return an instance of the object if the object
//does not already exist.
public static function getInstance() {
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}
// The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
// thus eliminating the possibility of duplicate objects.
public function __clone() {
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
public function __wakeup() {
trigger_error('Deserializing is not allowed.', E_USER_ERROR);
}
// private constructor
private function __construct() {
parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
if (mysqli_connect_error()) {
exit('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
parent::set_charset('utf-8');
}
public function get_wisher_id_by_name($name) {
$name = $this->real_escape_string($name);
$wisher = $this->query("SELECT id FROM wishers WHERE name = '"
. $name . "'");
if ($wisher->num_rows > 0) {
$row = $wisher->fetch_row();
return $row[0];
} else
return null;
}
public function get_wishes_by_wisher_id($wisherID) {
return $this->query("SELECT id, description, due_date FROM wishes WHERE wisher_id=" . $wisherID);
}
public function create_wisher($name, $password) {
$name = $this->real_escape_string($name);
$password = $this->real_escape_string($password);
$this->query("INSERT INTO wishers (name, password) VALUES ('" . $name
. "', '" . $password . "')");
}
public function verify_wisher_credentials($name, $password) {
$name = $this->real_escape_string($name);
$password = $this->real_escape_string($password);
$result = $this->query("SELECT 1 FROM wishers WHERE name = '"
. $name . "' AND password = '" . $password . "'");
return $result->data_seek(0);
}
}
?>[/php]
My code:
[php]<?php
class WishDB extends mysqli {
// single instance of self sahred among all instances
private static $instance = null;
// db connection config vars
private $user = "phpuser";
private $pass = "phpuserpw";
private $dbName = "wishlist";
private $dbHost = "localhost";
private $con = null;
// This method must be static, and must return an instance of the object if the object
// does not already exist.
public static function getInstance() {
if (!self::$instance instanceof self){
self::$instance = new self;
}
return self::$instance;
}
// The clone and wakeup methods prevent external instantiation of copies of the Singleton class,
// thus eliminating the possibility of duplicate objects.
public function _clone() {
trigger_error('Clone is not allowed', E_USER_ERROR);
}
public function _wakeup() {
trigger_error('Deserializing is not allowed.', E_USER_ERROR);
}
// private constructor
private function _construct() {
parent::_construct($this->dbHost, $this->user, $this->pass, $this->dbName);
if (mysqli_connect_error()){
exit('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
parent::set_charset('utf-8');
}
public function get_wisher_id_by_name($name){
$name = $this->real_escape_string($name);
$wisher = $this->query("SELECT id FROM wishers WHERE name='" . $name . "'");
if ($wisher->num_rows > 0){
$row = $wisher->fetch_row();
return $row[0];
} else
return null;
}
public function get_wisher_by_wisher_id($wisehrID) {
return $this->query("SELECT id, description, due_date FROM wishes WHERE wisher_id=" . $wisherID);
}
public function create_wisher ($name, $password){
$name = $this->real_escape_string($name);
$password = $this->real_escape_string($password);
$this->query("INSERT INTO wishers (name,password) VALUES ('" . $name . "', '" . $password . "')");
}
public function verify_wisher_credentials ($name, $password) {
$name = $this->real_escape_string($name);
$password = $this->real_escape_string($password);
$result = $this->query("SELECT 1 FROM wishers WHERE name = '"
. $name . "' AND password = '" . $password . "'");
return $result->data_seek(0);
}
}
?>
[/php]
I wanna know what I’m doing wrong it’s driving me crazy. I know I can just copy the code from the tut’s and it works and continue on, but I want to learn what I broke.