0

I'm having issues binding ORDER BY and the direction in PDO. From what I've seen online, you need to bind SQL operators to the query but isn't that what I'm doing with my params array? It seems that I'm doing everything properly here so I don't understand why it's not working.

/**
 * Runs a query on the database
 * @param  mixed $params Array containing query and bind params
 * @return mixex         Raw database object
 */
public static function runQuery($params) {
    // Prepare Query
    $preparedQuery = Database::connection()->prepare($params["query"]);

    // Execute Query
    $preparedQuery->execute($params["params"]);

    // Return results
    return $preparedQuery;
}

/**
 * Lists all values from a table
 * @param  string $table What table to query
 * @return array            Associative array from specific table with all values
 */
public static function getAll($table, $order, $direction) {
    // List of results from database
    $data = Utilities::runQuery([
        "query" => "SELECT * from `$table` ORDER BY :order :direction",
        "params" => [
            ":order" => $order,
            ":direction" => $direction
        ]
    ]);

    // Return list
    return $data->fetchAll(PDO::FETCH_ASSOC);
}
Joe Scotto
  • 10,936
  • 14
  • 66
  • 136
  • @smith I'm not using a framework. – Joe Scotto Apr 05 '18 at 02:05
  • Possible duplicate of [How do I set ORDER BY params using prepared PDO statement?](https://stackoverflow.com/questions/2542410/how-do-i-set-order-by-params-using-prepared-pdo-statement) –  Apr 05 '18 at 02:14

1 Answers1

0

In a ORDER BYclause, I think you can't use placeholders.

Only values can be binded, not column names.

LBS
  • 518
  • 8
  • 17