PHP-MySQL database connection using namespaces (ok)

C:\xampp\htdocs\namespace\demo.php

<?php 
/**
 * Location demo.php
 */
require("config.php");
use DB\MySQL\Query as query;
$users = new query("select * from users");
$all_users = $users->fetch();
echo "<strong>All Users </strong>" . "<br/>";
echo '<pre>';
	var_export($all_users);
echo '</pre>';
echo "<strong>First Users</strong>" . "<br/>";
$first_user = $users->first();
echo '<pre>';
	var_export($first_user);
echo '</pre>';
echo "<strong>Last Users</strong>" . "<br/>";
$last_user = $users->last();
echo '<pre>';
	var_export($last_user);
echo '</pre>';
 ?>

C:\xampp\htdocs\namespace\config.php

<?php
/**
 * Location /config.php
 */
define("NAMESPACE_DIR", 'import');
function __autoload($class) {
  $parts    = explode("\\", $class);
  $filepath = implode(DIRECTORY_SEPARATOR, $parts);
  $filepath = strtolower(NAMESPACE_DIR . DIRECTORY_SEPARATOR . $filepath . '.php');
  require $filepath;
}
$DB_SETTINGS = array(
  'host'     => 'localhost',
  'database' => 'curd',
  'username' => 'root',
  'password' => '',
);
global $DB_SETTINGS;

C:\xampp\htdocs\namespace\import\db.php

<?php 
namespace DB;
class DB{
	function __construct(){
		
	}
}
?>

C:\xampp\htdocs\namespace\import\db\mysql.php

<?php
/**
 * Location import\db\mysql.php
 */
namespace DB;
class MySQL {
  protected $host;
  protected $database;
  protected $username;
  protected $password;
  private $resource_id;
  public $connection = NULL;
  function __construct($settings = array()) {
    $this->host     = $settings['host'];
    $this->database = $settings['database'];
    $this->username = $settings['username'];
    $this->password = $settings['password'];
    if (is_null($this->connection)) {
      $this->getConnection()->connectDB();
    }
    return $this->connection;
  }
  function __destruct() {
    @mysqli_close($this->connection);
  }
  public function getConnection() {
    $this->connection = @mysqli_connect(
      $this->host,
      $this->username,
      $this->password
    );
    if (!$this->connection) {
      die("Unable to create connection, Server might be too busy or check your credentials!");
    }
    return $this;
  }
  public function connectDB() {
    if (mysqli_select_db($this->connection, $this->database)) {
      return $this;
    } else {
      die("Unable to connect to database");
    }
  }
}
?>

C:\xampp\htdocs\namespace\import\db\mysql\query.php

<?php
/**
 * Location db\mysql\query.php
 */
namespace DB\Mysql;
use DB\Mysql as mysql;
class Query extends mysql {
  private $resource_id;
  private $record_set;
  function __construct($sql) {
    global $DB_SETTINGS;
    $this->record_set  = Array();
    $this->resource_id = parent::__construct($DB_SETTINGS);
    $this->query($sql);
  }
  public function query($sql) {
    $this->resource_id = mysqli_query($this->connection, $sql);
    if (!$this->resource_id) {
      echo (mysql_error());
      return null;
    }
    return $this;
  }
  public function fetch() {
    $record_set = Array();
    while ($row = mysqli_fetch_assoc($this->resource_id)) {
      $record_set[] = $row;
    }
    return $record_set;
  }
  /** other utility functions **/
  public function first() {
    mysqli_data_seek($this->resource_id, 0);
    $record_set = Array();
    $row        = mysqli_fetch_assoc($this->resource_id);
    if ($row) {
      $record_set = $row;
    }
    return $record_set;
  }
  public function last() {
    mysqli_data_seek($this->resource_id, 0);
    $record_set = Array();
    while ($row = mysqli_fetch_assoc($this->resource_id)) {
      $record_set = $row;
    }
    return $record_set;
  }
}
?>

Last updated