Need help comparing my Code to Tutorial's code.

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.

Also here are my errors I get with my code.

( ! ) Warning: mysqli::real_escape_string(): invalid object or resource WishDB in C:\xampp\htdocs\wishlist\Includes\db.php on line 60
Call Stack

Time Memory Function Location

1 0.0007 141080 {main}( ) …\index.php:0
2 0.0011 168360 WishDB->verify_wisher_credentials( ) …\index.php:7
3 0.0011 168432 mysqli->real_escape_string( ) …\db.php:60

( ! ) Warning: mysqli::real_escape_string(): invalid object or resource WishDB in C:\xampp\htdocs\wishlist\Includes\db.php on line 61
Call Stack

Time Memory Function Location

1 0.0007 141080 {main}( ) …\index.php:0
2 0.0011 168360 WishDB->verify_wisher_credentials( ) …\index.php:7
3 0.0091 168840 mysqli->real_escape_string( ) …\db.php:61

( ! ) Warning: mysqli::query(): invalid object or resource WishDB in C:\xampp\htdocs\wishlist\Includes\db.php on line 63
Call Stack

Time Memory Function Location

1 0.0007 141080 {main}( ) …\index.php:0
2 0.0011 168360 WishDB->verify_wisher_credentials( ) …\index.php:7
3 0.0170 168968 mysqli->query( ) …\db.php:63

( ! ) Fatal error: Call to a member function data_seek() on a non-object in C:\xampp\htdocs\wishlist\Includes\db.php on line 64
Call Stack

Time Memory Function Location

1 0.0007 141080 {main}( ) …\index.php:0
2 0.0011 168360 WishDB->verify_wisher_credentials( ) …\index.php:7

I figured it out I put _clone() , _wakeup(), and construct() instead of [b][/b]clone(), [b][/b]wakeup(), and [b][/b]_construct. The errors confused me. This was for Creating a Database Driven Application With PHP Lesson 5: Adding Security. Implementing Application User Logon by the way. Just incase someone else goofs like I did and tries and search for a solution.

Sponsor our Newsletter | Privacy Policy | Terms of Service