<?php
require 'db-connection.php';
function get_fatch_row($table, $where = '', $order = '')
{
    global $link;
    if (!empty($where)) {
        $where = mysqli_real_escape_string($link, $where);
    }
    if (!empty($order)) {
        $order = mysqli_real_escape_string($link, $order);
    }
    $sql = "SELECT * FROM `$table`";
    if (!empty($where)) {
        $sql .= " WHERE $where";
    }
    if (!empty($order)) {
        $sql .= " ORDER BY $order";
    }
    $result = mysqli_query($link, $sql);
    if (!$result) {
        die("Error executing query: " . mysqli_error($link));
    }
    if ($result && mysqli_num_rows($result) > 0) {
        return mysqli_fetch_assoc($result);
    } else {
        return null;
    }
}
function get_fetch_rows($table, $where = '', $limit = '', $order = '')
{
    global $link;
    $sql = "SELECT * FROM $table";
    if (!empty($where)) {
        $sql .= " WHERE $where";
    }
    if (!empty($order)) {
        $sql .= " ORDER BY $order";
    }
    if (!empty($limit)) {
        $sql .= " LIMIT $limit";
    }
    // return $sql;
    $result = mysqli_query($link, $sql);
    if ($result && mysqli_num_rows($result) > 0) {
        $rows = [];
        while ($row = mysqli_fetch_assoc($result)) {
            $rows[] = $row;
        }
        return $rows;
    } else {
        return [];
    }
}

function delete_row($table, $where)
{
    global $link;
    $sql = "DELETE FROM $table WHERE $where";
    if (mysqli_query($link, $sql)) {
        return true;
    } else {
        return false;
    }
}