You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
447 lines
13 KiB
PHP
447 lines
13 KiB
PHP
<?php
|
|
|
|
namespace NN\Module;
|
|
|
|
class DB {
|
|
|
|
public $host;
|
|
public $user;
|
|
public $pass;
|
|
public $db;
|
|
|
|
public function __construct(...$arg){
|
|
// $this->host = 'localhost';
|
|
// $this->user = 'absensi';
|
|
// $this->pass = 'Feed$123$';
|
|
// $this->db = 'admin_absensi';
|
|
$this->host = HOST;
|
|
$this->user = USERNAME;
|
|
$this->pass = PASSWORD;
|
|
$this->db = DATABASE;
|
|
|
|
// $this->host = '192.168.0.102';
|
|
// $this->user = 'root';
|
|
// $this->pass = '';
|
|
// $this->db = 'absensi';
|
|
|
|
}
|
|
|
|
private static function cekDatbase(){
|
|
|
|
$conn = mysqli_connect((new self)->host, (new self)->user, (new self)->pass);
|
|
if ($conn) {
|
|
$cekDb = mysqli_select_db($conn, (new self)->db);
|
|
if ($cekDb) {
|
|
return "tersedia";
|
|
}else{
|
|
$queryCreateDb = mysqli_query($conn, "CREATE DATABASE ".(new self)->db);
|
|
if ($queryCreateDb) {
|
|
return "dibuat";
|
|
}
|
|
}
|
|
}else{
|
|
return "this not connect";
|
|
}
|
|
}
|
|
|
|
public static function cekd(){
|
|
echo "string";
|
|
}
|
|
|
|
|
|
private static function getDepartment(){
|
|
return mysqli_connect('145.223.22.181', (new self)->user, (new self)->pass, (new self)->db, 3307);
|
|
}
|
|
|
|
public static function cekconnection(){
|
|
echo "connection";
|
|
}
|
|
|
|
public static function dbquery($qr ='', $type=""){
|
|
$getConnection = (new self)->getDepartment();
|
|
$query = mysqli_query($getConnection, $qr);
|
|
$box = [];
|
|
if($query !== false){
|
|
while ($data = mysqli_fetch_object($query) ) {
|
|
$box[] = $data;
|
|
}
|
|
if ($type == "count") {
|
|
return count($box);
|
|
}else{
|
|
return $box;
|
|
}
|
|
}else{
|
|
return [
|
|
"reponse" => 0,
|
|
"query" => $qr
|
|
];
|
|
}
|
|
}
|
|
|
|
public static function getColumnName($table = '', $row = ''){
|
|
$data = (new self)->dbquery("
|
|
SELECT
|
|
COLUMN_NAME as nama_kolom
|
|
FROM
|
|
information_schema. COLUMNS
|
|
WHERE
|
|
TABLE_SCHEMA = '".(new self)->db."'
|
|
AND TABLE_NAME = '".$table."'
|
|
AND ORDINAL_POSITION = ".$row."
|
|
");
|
|
$nama = "";
|
|
foreach ($data as $key => $value) {
|
|
$nama .= $value->nama_kolom;
|
|
}
|
|
|
|
return $nama;
|
|
}
|
|
|
|
public static function ArrColumnName($table = ''){
|
|
$data = (new self)->dbquery("
|
|
SELECT
|
|
COLUMN_NAME as nama_kolom
|
|
FROM
|
|
information_schema. COLUMNS
|
|
WHERE
|
|
TABLE_SCHEMA = '".(new self)->db."'
|
|
AND TABLE_NAME = '".$table."'
|
|
");
|
|
$nama = array();
|
|
foreach ($data as $key => $value) {
|
|
$nama[] = $value->nama_kolom;
|
|
}
|
|
|
|
return $nama;
|
|
}
|
|
|
|
public static function cekColumn($table = '', $row = ''){
|
|
return (new self)->dbquery("
|
|
SELECT
|
|
COLUMN_NAME as nama_kolom
|
|
FROM
|
|
information_schema. COLUMNS
|
|
WHERE
|
|
TABLE_SCHEMA = '".(new self)->db."'
|
|
AND TABLE_NAME = '".$table."'
|
|
AND ORDINAL_POSITION = ".$row."
|
|
", "count");
|
|
}
|
|
|
|
public static function cekTable($table = '', $tablestruktur = ''){
|
|
$getConnection = (new self)->getDepartment();
|
|
$query = mysqli_query($getConnection, "DESCRIBE $table ");
|
|
if ($query) {
|
|
|
|
$aa = (new self)->ArrColumnName($table);
|
|
$bb = array_keys($tablestruktur);
|
|
|
|
if (count($aa) > count($bb)) {
|
|
foreach ($aa as $ay => $ax) {
|
|
if (in_array($ax, $bb)) {
|
|
}else{
|
|
(new self)->query("
|
|
ALTER TABLE ".$table."
|
|
DROP COLUMN ".$ax.";
|
|
");
|
|
}
|
|
}
|
|
}else{
|
|
$no = 1;
|
|
foreach ($tablestruktur as $key => $value) {
|
|
if ((new self)->cekColumn($table, $no) == 0) {
|
|
(new self)->query("
|
|
|
|
ALTER TABLE ".$table."
|
|
ADD ".$key." ".$value.";
|
|
");
|
|
}else{
|
|
if ((new self)->getColumnName($table, $no) != $key) {
|
|
(new self)->query("
|
|
|
|
ALTER TABLE ".$table."
|
|
CHANGE COLUMN ".(new self)->getColumnName($table, $no)." ".$key." ".$value.";
|
|
");
|
|
}
|
|
}
|
|
$no++;
|
|
}
|
|
}
|
|
return 'tersedia';
|
|
}else{
|
|
$mystructure = "";
|
|
$no = 0;
|
|
foreach ($tablestruktur as $key => $value) {
|
|
if ($no == 0) {
|
|
$mystructure .= $key.' '.$value;
|
|
}else{
|
|
$mystructure .= ','.$key.' '.$value;
|
|
}
|
|
$no++;
|
|
}
|
|
$createtable = mysqli_query($getConnection, 'CREATE TABLE '.$table.' ('.$mystructure.') ');
|
|
if ($createtable) {
|
|
return 'dibuat';
|
|
}else{
|
|
return 'gagal';
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// query data ke database
|
|
public static function query($e = '')
|
|
{
|
|
$conn = (new self)->getDepartment();
|
|
$query = mysqli_query($conn, $e);
|
|
$response = [];
|
|
$response['affected_row'] = mysqli_affected_rows($conn);
|
|
$response['error'] = mysqli_error($conn);
|
|
|
|
// Jika tidak ada error dan affected rows 0, return array kosong
|
|
if (empty($response['error']) && $response['affected_row'] == 0) {
|
|
return json_encode([]);
|
|
}
|
|
|
|
return json_encode($response);
|
|
}
|
|
|
|
public static function cekruno(){
|
|
echo "iam run";
|
|
}
|
|
|
|
// ambuil data secara objek
|
|
public static function query_result_object($e = '')
|
|
{
|
|
$conn = (new self)->getDepartment();
|
|
// echo $e;
|
|
// die();
|
|
$query = mysqli_query($conn, $e);
|
|
$box = [];
|
|
if($query != false){
|
|
while ($data = mysqli_fetch_object($query) ) {
|
|
$box[] = $data;
|
|
}
|
|
}
|
|
return $box;
|
|
}
|
|
|
|
public static function query_result_object_row($e = '')
|
|
{
|
|
$conn = (new self)->getDepartment();
|
|
$query = mysqli_query($conn, $e);
|
|
$box = [];
|
|
while ($data = mysqli_fetch_object($query) ) {
|
|
$box[] = $data;
|
|
}
|
|
return isset($box[0]) ? $box[0] : null;
|
|
}
|
|
|
|
// ambil data secara arrray
|
|
|
|
public function query_result_array($e = "")
|
|
{
|
|
$conn = (new self)->getDepartment();
|
|
$query = mysqli_query($conn, $e);
|
|
$box = [];
|
|
while ($data = mysqli_fetch_array($query) ) {
|
|
$box[] = $data;
|
|
}
|
|
return $box;
|
|
}
|
|
|
|
public function get_table($table='')
|
|
{
|
|
return (new self)->query_result_array("SELECT * FROM `".$table."`");
|
|
}
|
|
|
|
|
|
public static function query_result_assoc($e)
|
|
{
|
|
$conn = (new self)->getDepartment();
|
|
$query = mysqli_query($conn, $e);
|
|
$box = [];
|
|
while ($data = mysqli_fetch_assoc($query) ) {
|
|
$box[] = $data;
|
|
}
|
|
return $box;
|
|
}
|
|
// hitung total query data
|
|
public static function count_query($e)
|
|
{
|
|
$conn = (new self)->getDepartment();
|
|
$query = mysqli_query($conn, $e);
|
|
$box = [];
|
|
while ($data = mysqli_fetch_object($query) ) {
|
|
$box[] = $data;
|
|
}
|
|
return count($box);
|
|
}
|
|
// nah ini rumusnya tadi
|
|
public static function sql_like_table($arr, $search){
|
|
$table_row_data = "";
|
|
$table_row_data .= "(";
|
|
foreach ($arr as $key => $value) {
|
|
if ($key == 0) {
|
|
$table_row_data .= $value." LIKE '%".$search."%' ";
|
|
}else{
|
|
$table_row_data .= ' OR '.$value." LIKE '%".$search."%' ";
|
|
}
|
|
}
|
|
$table_row_data .= ")";
|
|
return $table_row_data;
|
|
}
|
|
|
|
public static function sql_order_table($arr, $order){
|
|
if ($order != "") {
|
|
$columnName = "";
|
|
foreach ($arr as $key => $nilaicolumn) {
|
|
if ($key == $order[0]["column"]) {
|
|
$columnName = $nilaicolumn;
|
|
}
|
|
}
|
|
$columnOrder = $_POST["order"][0]["dir"];
|
|
$order = 'ORDER BY '.$columnName.' '.$columnOrder.' ';
|
|
}else{
|
|
$order = ' ORDER BY id DESC ';
|
|
}
|
|
|
|
return $order;
|
|
}
|
|
|
|
public static function insert($table = "", $data_arr = []){
|
|
$conn = (new self)->getDepartment();
|
|
|
|
$data = "data saya ok";
|
|
$keys = array_keys($data_arr);
|
|
$name_of_query = "INSERT INTO ";
|
|
$namaTable = $table;
|
|
$data_keys = " (";
|
|
foreach ($keys as $key => $nilai_key) {
|
|
if ($key == 0) {
|
|
$data_keys .= $nilai_key;
|
|
}else{
|
|
$data_keys .= ','.$nilai_key;
|
|
}
|
|
}
|
|
$data_keys .= ")";
|
|
$data_keys .= " VALUES ";
|
|
$nilai_data = "(";
|
|
for ($i=0; $i < count($data_arr); $i++) {
|
|
if ($i == 0) {
|
|
$nilai_data .= '"'.$data_arr[$keys[$i]].'"';
|
|
}else{
|
|
$nilai_data .= ',"'.$data_arr[$keys[$i]].'"';
|
|
}
|
|
}
|
|
$nilai_data .= ")";
|
|
$nilai_query = $name_of_query.$namaTable.$data_keys.$nilai_data;
|
|
$query = mysqli_query($conn, $nilai_query);
|
|
return $query;
|
|
}
|
|
|
|
public static function sql_update_query($table, $data_arr, $where){
|
|
$conn = (new self)->getDepartment();
|
|
|
|
$data = "data saya ok";
|
|
$keys = array_keys($data_arr);
|
|
$keys2 = array_keys($where);
|
|
$name_of_query = "UPDATE ";
|
|
$namaTable = $table;
|
|
$nilai_data = " SET ";
|
|
for ($i=0; $i < count($data_arr); $i++) {
|
|
if ($i == 0) {
|
|
$nilai_data .= $keys[$i].' = "'.$data_arr[$keys[$i]].'"';
|
|
}else{
|
|
$nilai_data .= ', '.$keys[$i].' = "'.$data_arr[$keys[$i]].'"';
|
|
}
|
|
}
|
|
$argument = " WHERE ";
|
|
for ($y=0; $y < count($where); $y++) {
|
|
if ($y == 0) {
|
|
$argument .= $keys2[$y]." = '".$where[$keys2[$y]]."' ";
|
|
}else{
|
|
$argument .= " AND ".$keys2[$y]." = '".$where[$keys2[$y]]."' ";
|
|
}
|
|
}
|
|
$nilai_query = $name_of_query.$namaTable.$nilai_data.$argument;
|
|
$query = mysqli_query($conn, $nilai_query);
|
|
return $query;
|
|
}
|
|
|
|
public static function sql_delete_query($table, $where){
|
|
$conn = (new self)->getDepartment();
|
|
$keys2 = array_keys($where);
|
|
$argument = " WHERE ";
|
|
for ($y=0; $y < count($where); $y++) {
|
|
if ($y == 0) {
|
|
$argument .= $keys2[$y]." = '".$where[$keys2[$y]]."' ";
|
|
}else{
|
|
$argument .= " AND ".$keys2[$y]." = '".$where[$keys2[$y]]."' ";
|
|
}
|
|
}
|
|
$delete_query = "DELETE FROM ".$table.$argument;
|
|
|
|
$query = mysqli_query($conn, $delete_query);
|
|
return $query;
|
|
|
|
}
|
|
|
|
public static function multipleQuery($queries = []) {
|
|
$conn = (new self)->getDepartment();
|
|
$results = [];
|
|
$success = true;
|
|
|
|
// Start transaction
|
|
mysqli_begin_transaction($conn);
|
|
|
|
try {
|
|
// Check if input is string (multiple queries with semicolons)
|
|
if (is_string($queries)) {
|
|
// Enable multi_query
|
|
if (mysqli_multi_query($conn, $queries)) {
|
|
do {
|
|
// Store first result set
|
|
if ($result = mysqli_store_result($conn)) {
|
|
$results[] = [
|
|
'affected_rows' => mysqli_affected_rows($conn),
|
|
'error' => mysqli_error($conn),
|
|
'query' => $queries
|
|
];
|
|
mysqli_free_result($result);
|
|
}
|
|
} while (mysqli_next_result($conn));
|
|
}
|
|
} else {
|
|
// Process array of queries
|
|
foreach ($queries as $query) {
|
|
$result = mysqli_query($conn, $query);
|
|
if ($result === false) {
|
|
throw new \Exception(mysqli_error($conn));
|
|
}
|
|
$results[] = [
|
|
'affected_rows' => mysqli_affected_rows($conn),
|
|
'error' => mysqli_error($conn),
|
|
'query' => $query
|
|
];
|
|
}
|
|
}
|
|
|
|
// If all queries succeed, commit the transaction
|
|
mysqli_commit($conn);
|
|
return [
|
|
'success' => true,
|
|
'results' => $results
|
|
];
|
|
} catch (\Exception $e) {
|
|
// If any query fails, rollback the transaction
|
|
mysqli_rollback($conn);
|
|
return [
|
|
'success' => false,
|
|
'error' => $e->getMessage(),
|
|
'results' => $results
|
|
];
|
|
}
|
|
}
|
|
}
|