I am trying to improved my coding skills. I mainly work in web development. I had a "system" that I use in all my projects to fetch data from my DB to the browser. I tried an implementation of OOP, it works fine, but I dont think I did it correctly.
How can I optimized this code to make full use of an OOP design?
<?php
class DBX{
//---------------USER-----------------------------
static function GetUserByEmail($email){
$link = openlink();
$query = "SELECT * FROM users WHERE email = ? LIMIT 1";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "s", $email);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_assoc();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function GetUserByID($id){
$link = openlink();
$query = "SELECT * FROM users WHERE user_id = ? LIMIT 1";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "i", $id);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_assoc();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function GetUsers(){
$link = openlink();
$query = "SELECT * FROM users";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_all();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function GetClients(){
$link = openlink();
$l = 4;
$query = "SELECT * FROM users WHERE access_level_id = ?";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "i", $l);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_all();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function AddUser(
$f_name,
$l_name,
$ll_name,
$email,
$access_level,
$status,
$phone,
$joinDate,
$clientNum,
$hashedPW
){
$link = openlink();
$query = "INSERT INTO users (
name,
l_name,
ll_name,
phone,
email,
status,
access_level_id,
join_date,
contract_id,
password
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param(
$stmt,
"sssssiisss",
$f_name,
$l_name,
$ll_name,
$phone,
$email,
$status,
$access_level,
$joinDate,
$clientNum,
$hashedPW
);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function UpdateUser($id, $userInfo){
$link = openlink();
$query = "UPDATE users SET
name = ?,
l_name = ?,
ll_name = ?,
email = ?,
access_level_id = ?,
phone = ?,
status = ?,
join_date = ?,
contract_id = ?,
password = ?
WHERE user_id = ? LIMIT 1";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param(
$stmt,
"ssssisisssi",
$userInfo['name'],
$userInfo['l_name'],
$userInfo['ll_name'],
$userInfo['email'],
$userInfo['access_level_id'],
$userInfo['phone'],
$userInfo['status'],
$userInfo['join_date'],
$userInfo['contract_id'],
$userInfo['password'],
$id
);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = "false5";
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function UpdateUserPasswordByID($password, $id){
$link = openlink();
$query = "UPDATE users SET
password = ?
where user_id = ? LIMIT 1";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
} else {
mysqli_stmt_bind_param(
$stmt,
"si",
$password,
$id
);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
//---------------ACCESS LEVELS-----------------------------
static function GetAccessLevels(){
$link = openlink();
$query = "SELECT * FROM access_levels";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_all();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
//---------------Follow Ups-----------------------------
static function GetFUByUserID($id){
$link = openlink();
$query = "SELECT * FROM follow_up WHERE user_id = ? ORDER BY created_at DESC";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "i", $id);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_all();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function AddFu($targetID, $senderID, $type, $message){
$link = openlink();
$date = date("Y-m-d H:i:s");
$query = "INSERT INTO follow_up (user_id, created_by, created_at, type, message) VALUES (?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "iisis", $targetID, $senderID, $date, $type, $message);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
//-------REPURPUSING Pool
//---------------USER-----------------------------
static function QuickAddUser($email, $f_name, $l_name, $ll_names, $hashedPW){
$link = openlink();
$status = 1;
$accessLevel = 8;
$query = "INSERT INTO users (email, f_name, l_name, ll_name, password, status, access_level_id)
VALUES (?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param(
$stmt,
"sssssii",
$email,
$f_name,
$l_name,
$ll_names,
$hashedPW,
$status,
$accessLevel
);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function GetUserByUsername($username){
$link = openlink();
$query = "SELECT * FROM users WHERE username = ? LIMIT 1";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "s", $username);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_assoc();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function GetUsersByNames($string){
$link = openlink();
$string = "%".$string."%";
$query = "SELECT * FROM users WHERE f_name LIKE ? OR l_name LIKE ? OR ll_name LIKE ?";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "sss", $string, $string, $string);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_all();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function GetStudentsByGroupID($groupID){
$link = openlink();
$query = "SELECT * FROM users WHERE group_id = ? ORDER BY l_name DESC";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "i", $groupID);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_all();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function GetStudentsBySchoolYearID($yearID){
$link = openlink();
$query = "SELECT * FROM users WHERE grado_id = ? ORDER BY l_name DESC";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "i", $yearID);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_all();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function GetFamilyByChildID($userID){
$link = openlink();
$query = "SELECT * FROM family_student WHERE student_id = ?";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "i", $userID);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_all();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function GetChildrenByFamilyID($userID){
$link = openlink();
$query = "SELECT * FROM family_student WHERE family_member_id = ?";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "i", $userID);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_all();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function GetUserByIDArray($id){
$link = openlink();
$query = "SELECT * FROM users WHERE user_id = ? LIMIT 1";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "i", $id);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_all();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function GetUsersByAccessLevel($accessLevel){
$link = openlink();
$query = "SELECT * FROM users WHERE access_id = ? ORDER BY l_name DESC";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "i", $accessLevel);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_all();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function linkToStudentByTargetUserID($id, $studentID){
$link = openlink();
$query = "INSERT INTO family_student (student_id, family_member_id) VALUES (?, ?)";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
} else {
mysqli_stmt_bind_param(
$stmt,
"ii",
$studentID,
$id
);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function UnlinkStudent($id, $studentID){
$link = openlink();
$query = "DELETE FROM family_student WHERE student_id = ? AND family_member_id = ? LIMIT 1";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
} else {
mysqli_stmt_bind_param(
$stmt,
"ii",
$studentID,
$id
);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function AddStudentToGroup($groudID, $studentID){
$link = openlink();
$query = "UPDATE users SET
group_id = ?
where user_id = ? LIMIT 1";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
} else {
mysqli_stmt_bind_param(
$stmt,
"ii",
$groudID,
$studentID
);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function UpdateUserPhotoByID($photoLoc, $id){
$link = openlink();
$query = "UPDATE users SET
profile_photo = ?
where user_id = ? LIMIT 1";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
} else {
mysqli_stmt_bind_param(
$stmt,
"si",
$photoLoc,
$id
);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function UpdateUserStatusByID($id, $status){
$link = openlink();
if ($status) {
$tempStatus = 1;
} else {
$tempStatus = 0;
}
$query = "UPDATE users SET
status = ?
where user_id = ? LIMIT 1";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
} else {
mysqli_stmt_bind_param(
$stmt,
"ii",
$tempStatus,
$id
);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
//---------------SCHOOL GRADE YEARS-----------------------------
static function AddSchoolYear($yearName){
$link = openlink();
$status = 1;
$query = "INSERT INTO grados ( name, status) VALUES (?, ?)";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "si", $yearName, $status);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function GetSchoolYearGrades(){
$link = openlink();
$query = "SELECT * FROM grados";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_all();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function GetSchoolYearByID($id){
$link = openlink();
$query = "SELECT * FROM grados WHERE grados_id = ? LIMIT 1";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "i", $id);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$resultArray = $result->fetch_assoc();
$finalTest = $resultArray;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function UpdateSchoolYear($id, $year){
$link = openlink();
$query = "UPDATE grados SET
name = ?
WHERE grados_id = ? LIMIT 1";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
} else {
mysqli_stmt_bind_param(
$stmt,
"si",
$year['name'],
$id
);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
static function UpdateSchoolYearStatusByID($id, $status){
$link = openlink();
if ($status) {
$tempStatus = 1;
} else {
$tempStatus = 0;
}
$query = "UPDATE grados SET
status = ?
where grados_id = ? LIMIT 1";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
} else {
mysqli_stmt_bind_param(
$stmt,
"ii",
$tempStatus,
$id
);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
//---------------ACCESS LEVELS-----------------------------
static function AddAccessLevel($yearName){
$link = openlink();
$query = "INSERT INTO acess_levels (name) VALUES (?)";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $query)) {
//return false if there was an error
return false;
}else {
mysqli_stmt_bind_param($stmt, "s", $yearName);
if (mysqli_stmt_execute($stmt)) {
$result = $stmt->get_result(); // get the mysqli result
$finalTest = true;
} else {
$finalTest = false;
}
}
closeLink($stmt, $link);
return $finalTest;
}
}
//open link
include 'dbConnect.inc.php';
//closing link
function closeLink($stmt, $link){
mysqli_stmt_close($stmt);
mysqli_close($link);
}
//date("Y-m-d H:i:s")
?>
As you can see I repeat most of the code over and over, I think I could do one method for each CRUD and then somehow just pass the differences... any ideas??
mysqlifunctions/methods. Right now it is inconsistent. \$\endgroup\$$this. \$\endgroup\$