Jump to content

Recommended Posts

Hey, I'm trying to create an object oriented webpage, with just some basic features, like creating a user, logging in, admin restricted pages.

However I'm stuck at this point.

This is my code:

class_user.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . "/includes/class_database.php";
?>



<?php

class User {

    private $database;
    
    public    function __construct(MySqlDatabase $database) { //Type Hinting
    $this->database = $database;
    }
    
    public function find_all() {
    $result = $this->database->db_query("SELECT * FROM users");
    return $result;    
    }
    
    public function find_by_id($id=1) {
    $result = $this->database->db_query("SELECT * FROM users WHERE id={$id}");
    $final = mysqli_fetch_array($result);
    return $final;    
    }
}
?>

class_database.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . "/includes/values.php";
include_once $_SERVER['DOCUMENT_ROOT'] . "/includes/class_user.php";
?>

<?php

class MySqlDatabase extends MySQLi {
    
    function __construct() {
        //Check if constants are missing
        if (defined(!DB_USERNAME) || defined(!DB_SERVER) || 
            defined(!DB_PASSWORD) || defined(!DB_NAME)) {
            die("One or more of the database constants are missing: " . mysqli_errno);
            }
            
        //Establish connection if constants are present using the parent class
        parent::__construct(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
        
        //Echo error message if connection has failed
        if ($this->connect_errno) {
            die("Database connection has failed: " . $this->connect_errno);
            }
    
    
    }

    public function db_query($sql) {
    $result = mysqli_query($this->connection, $sql);
        if (!$result) {
            die("Database query failed: " . $this->errno);
        }
    return $result;
    
}

    public function query_prep($value) {
    $result = mysqli_real_escape_string($this->connection, $value);
        if (!$result) {
            die("Preparing query failed: " . $this->errno);
        }
    return $result;
}  
}
?>

Calling externally:

<?php
$database = new MySqlDatabase();
$user = new User($database);
$found = $user->find_all();
?>

This is the error I'm getting:

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\includes\class_database.php on line 28

Database query failed: 0

 

I've tried everything I can come up with, but still I'm stuck at this error.

defined(!DB_USERNAME) is the same as defined(0) I think you mean !defined('DB_USERNAME') (mind the ')

 

$result = $this->database->db_query("SELECT * FROM users WHERE id={$id}");

$final = mysqli_fetch_array($result);

 

You either fully abstract your database or you don't, not trying to use both or you'll still end up screwed. The same goes for:

 

__construct(MySqlDatabase $database)

 

In OO we have a rule: program to an interface, not an implementation. Thus something like:

 

interface DatabaseInterface {
    function connect($user, $pass, $host = 'localhost');
    function disconnect();
    function isConnected();
    function prepare($sql);
    function execute($sql = null, $params = array());
    function escape($value);
}

class MySQLDatabase implements DatabaseInterface {
    // implement the above methods
}

class UserGateway {
    private $db = null;
    
    function __construct(DatabaseInterface $db) {
        $this->db = $db;
    }
    
    function findById($id) {
        return new User(
            $this->db->select()->from('users')->where('id', $id);
        );
    }
    
    function fetchAll($count = null, $offset = null) {
        if($count != null && $offset != null) {
            return new UserList(
                $this->db->select()->from('users')->limit($count, $offset);
            );
        } else if($count != null) {
            return new UserList(
                $this->db->select()->from('users')->limit($count);
            );
        } else {
            return new UserList(
                $this->db->select()->from('users');
            );
        }
    }
}

defined(!DB_USERNAME) is the same as defined(0) I think you mean !defined('DB_USERNAME') (mind the ')

 

$result = $this->database->db_query("SELECT * FROM users WHERE id={$id}");

$final = mysqli_fetch_array($result);

 

You either fully abstract your database or you don't, not trying to use both or you'll still end up screwed. The same goes for:

 

__construct(MySqlDatabase $database)

 

In OO we have a rule: program to an interface, not an implementation. Thus something like:

 

interface DatabaseInterface {
    function connect($user, $pass, $host = 'localhost');
    function disconnect();
    function isConnected();
    function prepare($sql);
    function execute($sql = null, $params = array());
    function escape($value);
}

class MySQLDatabase implements DatabaseInterface {
    // implement the above methods
}

class UserGateway {
    private $db = null;
    
    function __construct(DatabaseInterface $db) {
        $this->db = $db;
    }
    
    function findById($id) {
        return new User(
            $this->db->select()->from('users')->where('id', $id);
        );
    }
    
    function fetchAll($count = null, $offset = null) {
        if($count != null && $offset != null) {
            return new UserList(
                $this->db->select()->from('users')->limit($count, $offset);
            );
        } else if($count != null) {
            return new UserList(
                $this->db->select()->from('users')->limit($count);
            );
        } else {
            return new UserList(
                $this->db->select()->from('users');
            );
        }
    }
}

So I should completely drop my style of coding right away?

Do you have any (dymmy friendly) websites that explain this implementation procedure?

Also, where do those very above functions come from, you aren't defining them, or was it just an example?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.