I\'m trying to create a function for my project. I would like it to handle all the check functions. What I mean with that is before you start inserting a row in your databas
You are on a very right track! Such a function should be an everyday companion for the every PHP programmer using mysqli, but strangely, only few ever have an idea of creating one.
I've had an exactly the same idea once and implemented a mysqli helper function of my own:
function prepared_query($mysqli, $sql, $params, $types = "")
{
$types = $types ?: str_repeat("s", count($params));
$stmt = $mysqli->prepare($sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();
return $stmt;
}
Main differences from your approach
With your example query it could be used like this
$check = prepared_query($sql, [$id, $mail])->get_result()->fetch_row();
or, if you want a distinct function, you can make it as well
function check_database($mysqli, $sql, $params, $types = "")
{
return prepared_query($mysqli, $sql, $params, $types)->get_result()->fetch_row();
}
and now it can be called as
$check = check_database($sql, [$id, $mail]);