I am making a method in my class, where the parameters for the method are $sql, $types, $values
.
function getResult($sql, $types, $values){
$stmt = $this->conn->prepare($sql);
$stmt->bind_param( "$types" , ...$values);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
return $result;
} else{
return "There is no such row";
}
}
But i wonder, maybe i could make a function where $types
are automatically generated based on the count of $values
and give it a string ("s"
). Something like this:
function getResult($sql, $values){
$stmt = $this->conn->prepare($sql);
$types = str_repeat("s", count($values));
$stmt->bind_param( $types, ...$values);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
return $result;
} else{
return "There is no such row";
}
}
Is it bad practise? It would make the code smaller