You've already forked OpenShorte.old
Website refactored
Converted MySQLi Database.php class to PHP_PDO objects
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
class database {
|
||||
class Database {
|
||||
private $config;
|
||||
private $connection;
|
||||
private $statement;
|
||||
@@ -14,31 +14,47 @@
|
||||
}
|
||||
|
||||
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." );
|
||||
try {
|
||||
$this->connection = new PDO ( "mysql:dbname=" . $this->config [ 'name' ] . ";host=" . $this->config [ 'host' ] . ":" . $this->config [ 'port' ], $this->config [ 'username' ], $this->config [ 'password' ] );
|
||||
}
|
||||
catch ( PDOException $exception ) {
|
||||
die ( "Database connection failed: " . $exception->getMessage ( ) );
|
||||
}
|
||||
}
|
||||
|
||||
public function query ( string $query ) {
|
||||
$result = $this->connection->query ( $query );
|
||||
if ( $result === false ) {
|
||||
die ( "Query execution error: " . $this->connection->errorInfo ( ) [ 2 ] );
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function prepare ( string $query ) : void {
|
||||
if ( ! ( $this->statement = $this->connection->prepare ( $query ) ) ) {
|
||||
die ( "Prepare failed: (" . $this->connection->errno . ") " . $this->connection->error );
|
||||
die ( "Prepare failed: " . $this->statement->errorInfo ( ) [ 2 ] );
|
||||
}
|
||||
}
|
||||
|
||||
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 );
|
||||
foreach ( $params as $paramKey => $paramValue ) {
|
||||
if ( ! $this->statement->bindValue ( $paramKey, $paramValue ) ) {
|
||||
die ( "Binding parameters failed: " . $this->statement->errorInfo ( ) [ 2 ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function execute ( ) : void {
|
||||
if ( ! $this->statement->execute ( ) ) {
|
||||
die ( "Execute failed: (" . $this->statement->errno . ") " . $this->statement->error );
|
||||
if ( $this->statement->execute ( ) === false ) {
|
||||
die ( "Execute failed: " . $this->statement->errorInfo ( ) [ 2 ] );
|
||||
}
|
||||
}
|
||||
|
||||
public function get_result ( ) : MySQLi_result {
|
||||
return $this->statement->get_result ( );
|
||||
public function debugDumpParams ( ) : void {
|
||||
$this->statement->debugDumpParams ( );
|
||||
}
|
||||
|
||||
public function get_result ( ) : PDOStatement {
|
||||
return $this->statement;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user