Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define("DB_HOST", "127.0.0.1");
- define("DB_USER", "root");
- define("DB_PASS", "");
- define("DB_NAME", "sample_db");
- $db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
- echo (!$db->connect_error) ? NULL : die("<pre>Unable to connect to the MySQL Server -> $db->connect_error</pre>");
- ?>
- <?php
- public $db;
- class MySQLqueries {
- public function samplefunction($queryString) {
- $sqlQry = mysqli->query($queryString);
- return ($sqlQry) ? "<pre>Query Executed Successfully</pre>" : die("<pre>An error occured -> $db->error</pre>");
- }
- }
- ?>
- <?php
- error_reporting(0);
- date_default_timezone_set("Asia/Manila");
- require 'connection.db.php';
- require 'sampleclass.class.php';
- ?>
- <?php
- require 'includes.inc.php';
- $todo = new MySQLqueries;
- echo $todo->samplefunction("SELECT `sample_column` FROM `sample_table` WHERE `sample_column` = 'sample_value';");
- ?>
- Class DB {
- function __construct($host, $user, $pass, $db) {
- return $this->connect($host, $user, $pass, $db);
- }
- function connect($host, $user, $pass, $db) {
- //..connect and all.
- }
- //...the rest of your functions/class...
- }
- Class Foo {
- $private $db;
- // your construct method here will ONLY except a `DB` class instance/object as $db.
- // Try it with anything else and learn from the errors to understand what I mean.
- function __construct(DB $db){
- $this->db = $db;
- }
- }
- $db = new DB($host, $user, $pass, $db);
- // you can error check it here
- $foo = new Foo($db);// inject the $db object.
- include('connection.db.php');
- class MySQLqueries {
- public function samplefunction($queryString) {
- global $db;
- $sqlQry = mysqli->query($queryString);
- return ($sqlQry) ? "<pre>Query Executed Successfully</pre>" : die("<pre>An error occured -> $db->error</pre>");
- }
- }
- class MysqlConn
- {
- public static $db;
- }
- MysqlConn::$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
- <?php
- class MysqlConn
- {
- protected
- static $DB_HOST = "127.0.0.1",
- static $DB_USER = "root",
- static $DB_PASS = "",
- static $DB_NAME = "sample_db";
- protected static $db;
- public static function getDB()
- {
- if (!isset(self::$db) {
- self::$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
- if ($db->connect_error) {
- die("<pre>Unable to connect to the MySQL Server -> $db->connect_error</pre>");
- }
- }
- return self::$db;
- }
- }
- MysqlConn::getDB();
- $db = MysqlConn::getDB();
- require_once 'connection.db.php';
- class MySQLqueries {
- public function samplefunction($queryString) {
- global $db;
- //You can now use $db variable as you want
- print_r($db);
- $sqlQry = mysqli->query($queryString);
- return ($sqlQry) ? "<pre>Query Executed Successfully</pre>" : die("<pre>An error occured -> $db->error</pre>");
- }
- }
- <?php
- class connection
- {
- public $servername = "localhost";
- public $username = "root";
- public $password = "root";
- public $dbname = "test";
- function __construct($servername, $username, $password, $dbname)
- {
- return $this->connect($servername, $username, $password, $dbname);
- }
- function connect($servername, $username, $password, $dbname)
- {
- $conn = new mysqli($servername, $username, $password, $dbname);
- }
- class cc extends connection
- {
- private $conn;
- function __construct(connection $conn)
- {
- $this->conn = $conn;
- }
- }
- }
- $conn = new connection($servername, $username, $password, $dbname);
- $obj = new connection();
- echo $obj->connect();
- $ccc = new cc($conn);// inject the $conn object.
- ?>
Add Comment
Please, Sign In to add comment