Add mysql-tabledump

This commit is contained in:
Dzejkobik007
2023-03-26 23:44:54 +02:00
parent c997fafc93
commit e4e447f5a3
2 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
<?php
/**
* IDB interface
* Interface for DB driver and basic I/U/D/S operations.
*/
interface IDB
{
/**
* Connect to DB on DBMS
*
* @param string $host hostname
* @param string $username username
* @param string $password password
* @param string $database database name
* @return static instance of DB on success
*/
public function connect(
string $host = "",
string $username = "",
string $password = "",
string $database = ""
): ?static;
/**
* SELECT rows from table
*
* @param string $query SQL query SELECT string
* @return array result as a associative array, key is attribute name, value is attribute array; empty on error
*/
function select(string $query): array;
/**
* INSERT record to table
*
* @param string $table database table name
* @param array $data data to insert, key is attribute name, value is attribute value
* @return boolean true on success otherwise false
*/
function insert(string $table, array $data): bool;
/**
* UPDATE record in table
*
* @param string $table database table name
* @param integer $id primary key value for record to update
* @param array $data data to update, key is attribute name, value is attribute value
* @return boolean true on success otherwise false
*/
function update(string $table, int $id, array $data): bool;
/**
* DELETE item from table
*
* @param string $table database table name
* @param integer $id primary key value for record to delete
* @return boolean true on success otherwise false
*/
function delete(string $table, int $id): bool;
}

View File

@@ -0,0 +1,63 @@
<?php
class MySQL implements IDB
{
private ?mysqli $db;
public function connect(
string $host = "",
string $username = "",
string $password = "",
string $database = ""
): ?static {
$db = mysqli_connect($host, $username, $password, $database);
if ($db === false) {
return null;
}
$this->db = $db;
return $this;
}
function select(string $query): array {
$result = mysqli_query($this->db,$query);
if (
$result instanceof mysqli_result
&& $result->num_rows > 0
){
return mysqli_fetch_all($result, MYSQLI_ASSOC);
}
return [];
}
function insert(string $table, array $data): bool {
$keys = "(";
$values = "(";
foreach(array_keys($data) as $key) {
$keys .= "'".$key."', ";
}
foreach($data as $row) {
$values .= "'".$row."', ";
}
$keys .= ")";
$values .= ")";
$query = "INSERT INTO `".$table."` ".$keys." VALUES ".$values;
$result = mysqli_query($this->db, $query);
return $result ? true : false;
}
function update(string $table, int $id, array $data): bool {
$query = "INSERT INTO `".$table."` VALUES (";
for($i = 0; $i < sizeof($data); $i++) {
$query .= "'".$data[$i]."', ";
}
$query .= ")";
$result = mysqli_query($this->db, $query);
return $result ? true : false;
}
function delete(string $table, int $id): bool {
$result = mysqli_query($this->db, "DELETE FROM ".$table." WHERE id=".$id);
return $result ? true : false;
}
}