0
0
This repository has been archived on 2020-11-15. You can view files and clone it, but cannot push or open issues or pull requests.
OpenShorte.old/lib/php/classes/Database.php
Bryan 81d21c311f
Minor changes
Moved config into $GLOBALS
Moved request_uri into $GLOBALS
Modified all files accordingly
Modified logout page for future improvements (not deleting the entire $_SESSION)
Fixed LICENSEs, moved LICENSE for PHP files in /lib/php subfolder instead of /lib
2019-04-06 11:57:35 +02:00

45 lines
1.8 KiB
PHP

<?php
class database {
private $config;
private $connection;
private $statement;
public function __construct ( $config ) {
if ( $config && $config [ 'host' ] && $config [ 'username' ] && $config [ 'password' ] && $config [ 'name' ] && $config [ 'port' ] ) {
$this->config = $config;
}
else {
die ( "You can't initialize a database connection without proper configuration." );
}
}
public function connect ( ) : void {
$this->connection = new MySQLi ( $this->config [ 'host' ], $this->config [ 'username' ], $this->config [ 'password' ], $this->config [ 'name' ], $this->config [ 'port' ] );
if ( $this->connection->connect_errno ) {
die ( "Database connection failed." );
}
}
public function prepare ( string $query ) : void {
if ( ! ( $this->statement = $this->connection->prepare ( $query ) ) ) {
die ( "Prepare failed: (" . $this->connection->errno . ") " . $this->connection->error );
}
}
public function bind ( array $params ) : void {
if ( ! call_user_func_array ( array ( $this->statement, "bind_param" ), array_merge ( $params [ 0 ], $params [ 1 ] ) ) ) {
die ( "Binding parameters failed: (" . $this->statement->errno . ") " . $this->statement->error );
}
}
public function execute ( ) : void {
if ( ! $this->statement->execute ( ) ) {
die ( "Execute failed: (" . $this->statement->errno . ") " . $this->statement->error );
}
}
public function get_result ( ) : MySQLi_result {
return $this->statement->get_result ( );
}
}