<?php
// get data or modify data
function getData($sql){
$db = new mysqli("localhost", "root", "", "database");
if($db->connect_errno != 0){
return $db->connect_error;
}
// for modify data:
// $db->query($sql);
$result = $db->query($sql);
if($db->errno != 0){
$db->close();
return $db->error;
}
// for modify data:
// return $db->affected_rows > 0 ? true : false;
$data = ($result->num_rows > 0) ? $result->fetch_all(MYSQLI_ASSOC) : [];
$db->close();
return $data;
}
?>
I have one PHP mysqli function for SELECT and another for INSERT/UPDATE/DELETE. Since they are very similar, should I combine them into one function or keep them separate?