Guest User

Untitled

a guest
Oct 25th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. <?php
  2. define("DB_HOST", "127.0.0.1");
  3. define("DB_USER", "root");
  4. define("DB_PASS", "");
  5. define("DB_NAME", "sample_db");
  6.  
  7. $db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  8.  
  9. echo (!$db->connect_error) ? NULL : die("<pre>Unable to connect to the MySQL Server -> $db->connect_error</pre>");
  10. ?>
  11.  
  12. <?php
  13. public $db;
  14.  
  15. class MySQLqueries {
  16. public function samplefunction($queryString) {
  17. $sqlQry = mysqli->query($queryString);
  18.  
  19. return ($sqlQry) ? "<pre>Query Executed Successfully</pre>" : die("<pre>An error occured -> $db->error</pre>");
  20. }
  21. }
  22. ?>
  23.  
  24. <?php
  25. error_reporting(0);
  26. date_default_timezone_set("Asia/Manila");
  27.  
  28. require 'connection.db.php';
  29. require 'sampleclass.class.php';
  30. ?>
  31.  
  32. <?php
  33. require 'includes.inc.php';
  34.  
  35. $todo = new MySQLqueries;
  36. echo $todo->samplefunction("SELECT `sample_column` FROM `sample_table` WHERE `sample_column` = 'sample_value';");
  37. ?>
  38.  
  39. Class DB {
  40.  
  41. function __construct($host, $user, $pass, $db) {
  42. return $this->connect($host, $user, $pass, $db);
  43. }
  44.  
  45. function connect($host, $user, $pass, $db) {
  46. //..connect and all.
  47. }
  48.  
  49. //...the rest of your functions/class...
  50. }
  51.  
  52. Class Foo {
  53.  
  54. $private $db;
  55.  
  56. // your construct method here will ONLY except a `DB` class instance/object as $db.
  57. // Try it with anything else and learn from the errors to understand what I mean.
  58. function __construct(DB $db){
  59. $this->db = $db;
  60. }
  61.  
  62. }
  63.  
  64. $db = new DB($host, $user, $pass, $db);
  65. // you can error check it here
  66.  
  67. $foo = new Foo($db);// inject the $db object.
  68.  
  69. include('connection.db.php');
  70.  
  71. class MySQLqueries {
  72. public function samplefunction($queryString) {
  73. global $db;
  74. $sqlQry = mysqli->query($queryString);
  75.  
  76. return ($sqlQry) ? "<pre>Query Executed Successfully</pre>" : die("<pre>An error occured -> $db->error</pre>");
  77. }
  78. }
  79.  
  80. class MysqlConn
  81. {
  82. public static $db;
  83. }
  84.  
  85. MysqlConn::$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  86.  
  87. <?php
  88. class MysqlConn
  89. {
  90. protected
  91. static $DB_HOST = "127.0.0.1",
  92. static $DB_USER = "root",
  93. static $DB_PASS = "",
  94. static $DB_NAME = "sample_db";
  95.  
  96. protected static $db;
  97.  
  98. public static function getDB()
  99. {
  100. if (!isset(self::$db) {
  101. self::$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  102.  
  103. if ($db->connect_error) {
  104. die("<pre>Unable to connect to the MySQL Server -> $db->connect_error</pre>");
  105. }
  106. }
  107. return self::$db;
  108. }
  109. }
  110.  
  111. MysqlConn::getDB();
  112.  
  113. $db = MysqlConn::getDB();
  114.  
  115. require_once 'connection.db.php';
  116.  
  117. class MySQLqueries {
  118. public function samplefunction($queryString) {
  119. global $db;
  120. //You can now use $db variable as you want
  121. print_r($db);
  122. $sqlQry = mysqli->query($queryString);
  123.  
  124. return ($sqlQry) ? "<pre>Query Executed Successfully</pre>" : die("<pre>An error occured -> $db->error</pre>");
  125. }
  126. }
  127.  
  128. <?php
  129. class connection
  130. {
  131.  
  132. public $servername = "localhost";
  133. public $username = "root";
  134. public $password = "root";
  135. public $dbname = "test";
  136. function __construct($servername, $username, $password, $dbname)
  137. {
  138. return $this->connect($servername, $username, $password, $dbname);
  139. }
  140.  
  141. function connect($servername, $username, $password, $dbname)
  142. {
  143. $conn = new mysqli($servername, $username, $password, $dbname);
  144. }
  145.  
  146. class cc extends connection
  147. {
  148. private $conn;
  149.  
  150. function __construct(connection $conn)
  151. {
  152. $this->conn = $conn;
  153. }
  154.  
  155. }
  156. }
  157. $conn = new connection($servername, $username, $password, $dbname);
  158. $obj = new connection();
  159. echo $obj->connect();
  160. $ccc = new cc($conn);// inject the $conn object.
  161.  
  162. ?>
Add Comment
Please, Sign In to add comment