Created
January 24, 2018 15:34
-
-
Save inxomnyaa/29af3c4d6db176eea3e8a8cf95bb2c79 to your computer and use it in GitHub Desktop.
Executes INSERT INTO, UPDATE, SELECT *, DELETE FROM WHERE with values of keys and DELETE FROM WHERE condition on databases using functions to auto-generate the query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @var \mysqli $db */ | |
private $db; | |
/** @var array */ | |
private $settings; | |
private $tablename = ""; | |
private $fieldarray = []; | |
public function __construct($settings = []){ | |
$this->settings = $settings; | |
$this->db = new \mysqli($settings['Host'], $settings['Username'], $settings['Password'], $settings['DatabaseName'], $settings['Port']); | |
$this->tablename = $settings['TableName']??$this->tablename; | |
} | |
public function insertRecord($fields = []){ | |
if(empty($fields)) return []; | |
/** @var \mysqli_stmt $stmt */ | |
$query = "INSERT INTO `" . $this->tablename . "` (`" . implode("`,`", array_keys($fields)) . "`) VALUES (" . implode(",", array_fill(0, count($fields) - 1, "?")) . ");"; | |
$stmt = $this->db->prepare($query); | |
var_dump($query); | |
$types = self::getTypes($fields); | |
$stmt->bind_param($types, ...$fields); | |
$result = $stmt->execute(); | |
if ($result === false){ | |
return []; | |
} | |
$result = $stmt->get_result(); | |
return $result->fetch_array(); | |
} | |
public function updateRecord($array = [], $fields = []){ | |
if(empty($fields)) return []; | |
/** @var \mysqli_stmt $stmt */ | |
$query = "UPDATE `" . $this->tablename . "` SET `" . implode("` = ?, `", array_keys($fields)) . "` = ? WHERE `" . implode("` = ? AND `", array_keys($array)) . "` = ?;"; | |
$stmt = $this->db->prepare($query); | |
var_dump($query); | |
$types = self::getTypes($array); | |
$types .= self::getTypes($fields); | |
$stmt->bind_param($types, ...$array, ...$fields); | |
$result = $stmt->execute(); | |
if ($result === false){ | |
return []; | |
} | |
$result = $stmt->get_result(); | |
return $result->fetch_array(); | |
} | |
public function getData($fields = []){ | |
if(empty($fields)) return []; | |
/** @var \mysqli_stmt $stmt */ | |
$query = "SELECT * FROM `" . $this->tablename . "` WHERE `" . implode("` = ? AND `", array_keys($fields)) . "` = ?;"; | |
$stmt = $this->db->prepare($query); | |
var_dump($query); | |
$types = self::getTypes($fields); | |
$stmt->bind_param($types, ...$fields); | |
$result = $stmt->execute(); | |
if ($result === false){ | |
return []; | |
} | |
$result = $stmt->get_result(); | |
return $result->fetch_array(); | |
} | |
public function deleteRecord($fields = []){ | |
if(empty($fields)) return []; | |
/** @var \mysqli_stmt $stmt */ | |
$query = "DELETE FROM `" . $this->tablename . "` WHERE `" . implode("` = ? AND `", array_keys($fields)) . "` = ?;"; | |
$stmt = $this->db->prepare($query); | |
var_dump($query); | |
$types = self::getTypes($fields); | |
$stmt->bind_param($types, ...$fields); | |
$result = $stmt->execute(); | |
if ($result === false){ | |
return []; | |
} | |
$result = $stmt->get_result(); | |
return $result->fetch_array(); | |
} | |
public function deleteSelection($fields = []){ | |
if(empty($fields)) return []; | |
/** @var \mysqli_stmt $stmt */ | |
$query = "DELETE FROM `" . $this->tablename . "` WHERE " . implode(" AND ", array_keys($fields)) . ";"; | |
$stmt = $this->db->prepare($query); | |
var_dump($query); | |
$types = self::getTypes($fields); | |
$stmt->bind_param($types, ...$fields); | |
$result = $stmt->execute(); | |
if ($result === false){ | |
return []; | |
} | |
$result = $stmt->get_result(); | |
return $result->fetch_array(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment