Website refactored
Converted MySQLi Database.php class to PHP_PDO objects
This commit is contained in:
parent
71e028cfdb
commit
63689ae0e9
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
class database {
|
class Database {
|
||||||
private $config;
|
private $config;
|
||||||
private $connection;
|
private $connection;
|
||||||
private $statement;
|
private $statement;
|
||||||
@ -14,31 +14,47 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function connect ( ) : void {
|
public function connect ( ) : void {
|
||||||
$this->connection = new MySQLi ( $this->config [ 'host' ], $this->config [ 'username' ], $this->config [ 'password' ], $this->config [ 'name' ], $this->config [ 'port' ] );
|
try {
|
||||||
if ( $this->connection->connect_errno ) {
|
$this->connection = new PDO ( "mysql:dbname=" . $this->config [ 'name' ] . ";host=" . $this->config [ 'host' ] . ":" . $this->config [ 'port' ], $this->config [ 'username' ], $this->config [ 'password' ] );
|
||||||
die ( "Database connection failed." );
|
|
||||||
}
|
}
|
||||||
|
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 {
|
public function prepare ( string $query ) : void {
|
||||||
if ( ! ( $this->statement = $this->connection->prepare ( $query ) ) ) {
|
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 {
|
public function bind ( array $params ) : void {
|
||||||
if ( ! call_user_func_array ( array ( $this->statement, "bind_param" ), array_merge ( $params [ 0 ], $params [ 1 ] ) ) ) {
|
foreach ( $params as $paramKey => $paramValue ) {
|
||||||
die ( "Binding parameters failed: (" . $this->statement->errno . ") " . $this->statement->error );
|
if ( ! $this->statement->bindValue ( $paramKey, $paramValue ) ) {
|
||||||
|
die ( "Binding parameters failed: " . $this->statement->errorInfo ( ) [ 2 ] );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute ( ) : void {
|
public function execute ( ) : void {
|
||||||
if ( ! $this->statement->execute ( ) ) {
|
if ( $this->statement->execute ( ) === false ) {
|
||||||
die ( "Execute failed: (" . $this->statement->errno . ") " . $this->statement->error );
|
die ( "Execute failed: " . $this->statement->errorInfo ( ) [ 2 ] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_result ( ) : MySQLi_result {
|
public function debugDumpParams ( ) : void {
|
||||||
return $this->statement->get_result ( );
|
$this->statement->debugDumpParams ( );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get_result ( ) : PDOStatement {
|
||||||
|
return $this->statement;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,21 +7,17 @@
|
|||||||
$link_id = (int) $link_id;
|
$link_id = (int) $link_id;
|
||||||
$database = new Database ( $GLOBALS [ 'config' ] [ 'db' ] );
|
$database = new Database ( $GLOBALS [ 'config' ] [ 'db' ] );
|
||||||
$database->connect ( );
|
$database->connect ( );
|
||||||
$database->prepare ( "SELECT links.URL FROM links WHERE links.ID = ?" );
|
$database->prepare ( "SELECT links.URL FROM links WHERE links.ID = :link_id" );
|
||||||
$parameters = [
|
$database->bind ( [ ':link_id' => $link_id ] );
|
||||||
[ "i" ],
|
|
||||||
[ &$link_id ],
|
|
||||||
];
|
|
||||||
$database->bind ( $parameters );
|
|
||||||
$database->execute ( );
|
$database->execute ( );
|
||||||
$result = $database->get_result ( );
|
$result = $database->get_result ( );
|
||||||
$row = $result->fetch_assoc ( );
|
if ( $result->rowCount ( ) == 0 ) {
|
||||||
if ( ! $row ) {
|
|
||||||
http_response_code ( 404 );
|
http_response_code ( 404 );
|
||||||
include ( $GLOBALS [ 'config' ] [ 'installation_path' ] . '/lib/errors/404.html' );
|
include ( $GLOBALS [ 'config' ] [ 'installation_path' ] . '/lib/errors/404.html' );
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
$row = $result->fetchAll ( ) [ 0 ];
|
||||||
?>
|
?>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
|
@ -22,17 +22,13 @@
|
|||||||
$database = new Database ( $GLOBALS [ 'config' ] [ 'db' ] );
|
$database = new Database ( $GLOBALS [ 'config' ] [ 'db' ] );
|
||||||
$database->connect ( );
|
$database->connect ( );
|
||||||
$url = $_POST [ 'url' ];
|
$url = $_POST [ 'url' ];
|
||||||
$database->prepare ( "INSERT INTO `links` ( `URL`, `created_by` ) VALUES ( ?, " . $_SESSION [ 'user_id' ] . " );" );
|
$database->prepare ( "INSERT INTO `links` ( `URL`, `created_by` ) VALUES ( :url, " . $_SESSION [ 'user_id' ] . " );" );
|
||||||
$parameters = [
|
$database->bind ( [ ':url' => $url ] );
|
||||||
[ "s" ],
|
|
||||||
[ &$url ],
|
|
||||||
];
|
|
||||||
$database->bind ( $parameters );
|
|
||||||
$database->execute ( );
|
$database->execute ( );
|
||||||
$database->prepare ( "SELECT MAX( links.ID ) as `last` FROM links;");
|
$database->prepare ( "SELECT MAX( links.ID ) as `last` FROM links;");
|
||||||
$database->execute ( );
|
$database->execute ( );
|
||||||
$result = $database->get_result ( );
|
$result = $database->get_result ( );
|
||||||
$row = $result->fetch_assoc ( );
|
$row = $result->fetchAll ( ) [ 0 ];
|
||||||
header ( 'Content-Type: application/json' );
|
header ( 'Content-Type: application/json' );
|
||||||
$response = [
|
$response = [
|
||||||
'status' => 200,
|
'status' => 200,
|
||||||
|
@ -12,16 +12,11 @@
|
|||||||
$password = $_POST [ 'password' ];
|
$password = $_POST [ 'password' ];
|
||||||
$database = new Database ( $config [ 'db' ] );
|
$database = new Database ( $config [ 'db' ] );
|
||||||
$database->connect ( );
|
$database->connect ( );
|
||||||
$database->prepare ( "SELECT users.id FROM users WHERE users.username = ? AND users.password = ?" );
|
$database->prepare ( "SELECT users.id FROM users WHERE users.username = :username AND users.password = :password" );
|
||||||
$parameters = [
|
$database->bind ( [ ':username' => $username, ':password' => $password ] );
|
||||||
[ "ss" ],
|
|
||||||
[ &$username, &$password ],
|
|
||||||
];
|
|
||||||
$database->bind ( $parameters );
|
|
||||||
$database->execute ( );
|
$database->execute ( );
|
||||||
$result = $database->get_result ( );
|
$result = $database->get_result ( );
|
||||||
$row = $result->fetch_assoc ( );
|
if ( $result->rowCount ( ) == 0 ) {
|
||||||
if ( ! $row ) {
|
|
||||||
header ( 'Content-Type: application/json' );
|
header ( 'Content-Type: application/json' );
|
||||||
http_response_code ( 401 );
|
http_response_code ( 401 );
|
||||||
$response = [
|
$response = [
|
||||||
@ -32,6 +27,7 @@
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
$row = $result->fetchAll ( ) [ 0 ];
|
||||||
$_SESSION [ 'user_id' ] = $row [ 'id' ];
|
$_SESSION [ 'user_id' ] = $row [ 'id' ];
|
||||||
header ( 'Content-Type: application/json' );
|
header ( 'Content-Type: application/json' );
|
||||||
$response = [
|
$response = [
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
$database->prepare ( "SELECT links.ID, links.URL FROM links WHERE links.created_by = " . $_SESSION [ 'user_id' ] );
|
$database->prepare ( "SELECT links.ID, links.URL FROM links WHERE links.created_by = " . $_SESSION [ 'user_id' ] );
|
||||||
$database->execute ( );
|
$database->execute ( );
|
||||||
$result = $database->get_result ( );
|
$result = $database->get_result ( );
|
||||||
$row = $result->fetch_assoc ( );
|
|
||||||
?>
|
?>
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
@ -19,13 +18,14 @@
|
|||||||
<button onclick="logout()">Logout</button>
|
<button onclick="logout()">Logout</button>
|
||||||
<div id="urllist">
|
<div id="urllist">
|
||||||
<?php
|
<?php
|
||||||
if ( ! $row ) {
|
if ( $result->rowCount ( ) == 0 ) {
|
||||||
echo ( "You have not created any URL yet.<br>" );
|
echo ( "You have not created any URL yet.<br>" );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
do {
|
$rows = $result->fetchAll ( PDO::FETCH_ASSOC );
|
||||||
|
foreach ( $rows as $row ) {
|
||||||
echo ( "\t\t\t" . '<div>' . $row [ 'ID' ] . " | " . $row [ 'URL' ] . "</div>\n" );
|
echo ( "\t\t\t" . '<div>' . $row [ 'ID' ] . " | " . $row [ 'URL' ] . "</div>\n" );
|
||||||
} while ( $row = $result->fetch_assoc ( ) );
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
</div>
|
</div>
|
||||||
|
Reference in New Issue
Block a user