- INSERT (Return last insert ID)
- UPDATE (Return boolean true on successfull udpate, false otherwise)
- SELECT (Return array or queried data)
- DELETE (Return boolean true on successfull udpate, false otherwise)
function query($query, $conn = false)
{
/*
* set the database connection globally
* or pass the database connection as second parameter in function call (optional)
*/
if(!$conn && trim($conn) == '') {
global $conn;
}
if(isset($query) && !empty($query)) {
$query = trim($query);
$result = mysqli_query($conn, $query);
$query_type = trim(strtolower(explode(" ", $query)[0]));
if($query_type == "select") {
if($result && mysqli_num_rows($result)>0) {
$data = [];
while($row = mysqli_fetch_assoc($result)) {
foreach($row as $key=>$val) {
$row[$key] = str_replace("\'", "'", $val);
}
$data[] = $row;
}
return $data;
}
}
elseif($query_type == "insert") {
$id = mysqli_insert_id($conn);
if($id) {
return $id;
}
}
elseif($query_type == "update" || $query_type == "delete") {
if($result && mysqli_affected_rows($conn)>0) {
return true;
}
}
}
return false;
}