Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arvindsvt/49c0d5a33434683ae0fbdf19846a7671 to your computer and use it in GitHub Desktop.
Save arvindsvt/49c0d5a33434683ae0fbdf19846a7671 to your computer and use it in GitHub Desktop.
sqlcrudephp-api-with-jwt-auth-blob-master
<?php
class Database
{
private $localhost = "localhost";
private $username = "root";
private $password = "";
private $database = "php_api_jwt";
private $mysqli = "";
private $result = array();
private $conn = false;
private static function getInstance(){
if (self::$instance == null){
$className = __CLASS__;
self::$instance = new $className;
//self::$instance = new static();
}
return self::$instance;
}
//connect database using consturcted method
public function __construct()
{
if (!$this->conn) {
$this->mysqli = new mysqli($this->localhost, $this->username, $this->password, $this->database);
$this->conn = true;
if ($this->mysqli->connect_error) {
array_push($this->result, $this->mysqli_connection_error);
return false;
}
} else {
return true;
}
}
// insert data
public function insert($table, $params = array())
{
if ($this->tableExist($table)) {
$table_column = implode(', ', array_keys($params));
$table_value = implode("', '", array_values($params));
$sql = "INSERT INTO $table ($table_column) VALUES ('$table_value')";
if ($this->mysqli->query($sql)) {
array_push($this->result, true);
return true;
} else {
array_push($this->result, false);
return false;
}
} else {
return false;
}
}
// get data
public function select($table, $row = "*", $join = null, $where = null, $order = null, $limit = null)
{
if ($this->tableExist($table)) {
$sql = "SELECT $row FROM $table";
if ($join != null) {
$sql .= " JOIN $join";
}
if ($where != null) {
$sql .= " WHERE $where";
}
if ($order != null) {
$sql .= " ORDER BY $order";
}
if ($limit != null) {
$sql .= " LIMIT $limit";
}
$query = $this->mysqli->query($sql);
if ($query) {
$this->result = $query->fetch_all(MYSQLI_ASSOC);
return true;
} else {
return false;
}
} else {
return false;
}
}
// update data
public function update($table, $params = array(), $where = null)
{
if ($this->tableExist($table)) {
$arg = array();
foreach ($params as $key => $val) {
$arg[] = "$key = '{$val}'";
}
$sql = "UPDATE $table SET " . implode(', ', $arg);
if($where != null){
$sql .=" WHERE $where";
}
if ($this->mysqli->query($sql)) {
array_push($this->result, true);
return true;
} else {
array_push($this->result, false);
return false;
}
} else {
return false;
}
}
// delete data
public function delete($table, $where = null)
{
if ($this->tableExist($table)) {
$sql = "DELETE FROM $table";
if ($where != null) {
$sql .= " WHERE $where";
}
if ($this->mysqli->query($sql)) {
array_push($this->result, true);
return true;
} else {
array_push($this->result, false);
return false;
}
} else {
return false;
}
}
// table exist
private function tableExist($table)
{
$sql = "SHOW TABLES FROM $this->database LIKE '{$table}'";
$tableInDb = $this->mysqli->query($sql);
if ($tableInDb) {
if ($tableInDb->num_rows == 1) {
return true;
} else {
array_push($this->result, $table . " Does not Exist");
}
} else {
return false;
}
}
// get result
public function getResult()
{
$val = $this->result;
$this->result = array();
return $val;
}
// close the connection
public function __destruct()
{
if ($this->conn) {
if ($this->mysqli->close()) {
$this->conn = false;
return true;
}
} else {
return false;
}
}
}
function insertOrUpdate($table,$params=array()){
global $conn;
$sqlQuery = 'CREATE TABLE IF NOT EXISTS '.$table.' (id INT NOT NULL AUTO_INCREMENT, ';
foreach($params as $field=>$val){
if($field == 'id'){ continue ; }
$sqlQuery .= ' '.$field.' VARCHAR( 250 ),';
}
$sqlQuery .= ' PRIMARY KEY ( `id` ));';
$sqlQuery .='INSERT INTO `'.$table.'` (`'.implode('`, `',array_keys($params)).'`) VALUES ("' . implode('", "', $params) . '")';
$args=array();
foreach($params as $field=>$value){
$args[]= $field.'=VALUES('.$field.')';
}
$sqlQuery .=' ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id) , '.implode(',',$args);
if ( mysqli_query($conn, $sqlQuery) ) {
return mysqli_insert_id($conn);
} else {
return false;
}
}
$DB = Database::getInstance()->insertData();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment