note 124646 deleted from class.mysqli by cmb
| From: | cmb@php.net | Date: | Thu, 07 May 2020 07:02:32 +0000 |
| Subject: | note 124646 deleted from class.mysqli by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-214587@lists.php.net to get a copy of this message | ||
Note Submitter: user at note dot com
----
<?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;
}
}
?>