note 124646 added to class.mysqli

From: Date: Fri, 24 Jan 2020 05:14:19 +0000
Subject: note 124646 added to class.mysqli
Groups: php.notes 
Request: Send a blank email to php-notes+get-213953@lists.php.net to get a copy of this message
<?php class Database{ protected $url; protected $user; protected $password; protected $db; protected $connection = null; public function __construct($url, $user, $password, $db) { $this->url = $url; $this->user = $user; $this->password = $password; $this->db = $db; } public function __destruct() { if($this->connection != null){ $this->closeConnection(); } } protected function makeConnection(){ $this->connection = new mysqli($this->url, $this->user, $this->password, $this->db); if($this->connection->connect_error){ echo "Fail" . $this->connection->connect_error; } } protected function closeConnection(){ if($this->connection != null){ $this->connection->close(); $this->connection = null; } } public function executeQuery($query, $params = null){ // Is there a DB connection? $this->makeConnection(); // Adjust query with params if available if($params != null){ // Change ? to values from parameter array $queryParts = preg_split("/\?/", $query); // if amount of ? is not equal to amount of values => error if(count($queryParts) != count($params) + 1){ return false; } // Add first part $finalQuery = $queryParts[0]; // Loop over all parameters for($i = 0; $i < count($params); $i++){ // Add clean parameter to query $finalQuery = $finalQuery . $this->cleanParameters($params[$i]) . $queryParts[$i + 1]; } $query = $finalQuery; } // Check for SQL injection $result = $this->connection->query($query); return $result; } protected function cleanParameters($parameters){ // prevent SQL injection $result = $this->connection->real_escape_string($parameters); return $result; } } ?> <?php include_once("Database.php"); class DatabaseFactory{ // Singleton private static $connection; public static function getDatabase(){ if(self::$connection == null){ $url = "dt5.ehb.be"; $user = "1920WEBINT038"; $passw = "94617528"; $db = "1920WEBINT038"; self::$connection = new Database($url, $user, $passw, $db); } return self::$connection; } } ?> <?php class Book{ public $bookId; public $title; public $releasedate; public $priceExclVAT; public $emailPublisher; public function __construct($bookId, $title, $releasedate, $priceExclVAT, $emailPublisher) { $this->bookId = $bookId; $this->title = $title; $this->releasedate = $releasedate; $this->priceExclVAT = $priceExclVAT; $this->emailPublisher = $emailPublisher; } } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.195 (proxied: 193.190.238.3) ---- Manual Page -- http://php.net/manual/en/class.mysqli.php Edit -- https://master.php.net/note/edit/124646 Del: integrated -- https://master.php.net/note/delete/124646/integrated Del: useless -- https://master.php.net/note/delete/124646/useless Del: bad code -- https://master.php.net/note/delete/124646/bad+code Del: spam -- https://master.php.net/note/delete/124646/spam Del: non-english -- https://master.php.net/note/delete/124646/non-english Del: in docs -- https://master.php.net/note/delete/124646/in+docs Del: other reasons-- https://master.php.net/note/delete/124646 Reject -- https://master.php.net/note/reject/124646 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#213953) next »