Using Mysqli, I have the following function:
public static function selectBy(Database $database, $columnName, $value){
$connection = $database->getConnection();
$stmt = $connection->prepare('
SELECT * FROM `Users`
WHERE ? = ?;');
$stmt->bind_param('ss', $columnName, $value);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_all()){
$finalResult[] = $row;
}
return $finalResult;
As you can see, it takes both the column name, and the value, and binds it to the prepared query, making the function dynamic to the developer to choose what value he wants to search and in what column of that specific table. The function works great if I change I fill the column name manually, for example:
$stmt = $connection->prepare('
SELECT * FROM `Users`
WHERE Username = ?;');
But as it is, using WHERE ? = ?, it doesn't work. Any ideas? I have checked the query many times, and enabled mysqli_report(MYSQLI_REPORT_ALL), but didn't get any error.
Thanks in advance.