db.php (867B)
1 <?php 2 # Sadly resources (such as database connections) can not be transferred across files via session 3 # So we need a class 4 class database { 5 private static $db; 6 private $connection; 7 8 # Connect with same credentials every time 9 # Root account used ONLY for your convenience --- I realize this is bad in practice 10 private function __construct() { 11 $this->connection = new mysqli('localhost', 'root', '', 'cosc4606_assignment_02'); 12 } 13 14 function __destruct() { 15 # Close connection or else we'd have a bunch of open connections 16 $this->connection->close(); 17 } 18 19 public static function get_connection() { 20 if (self::$db == null) { 21 self::$db = new database(); 22 } 23 return self::$db->connection; 24 } 25 } 26 ?>