Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set ORDER BY params using prepared PDO statement?

Tags:

php

mysql

pdo

I'm having problems using params in the ORDER BY section of my SQL. It doesn't issue any warnings, but prints out nothing.

$order = 'columnName'; $direction = 'ASC';  $stmt = $db->prepare("SELECT field from table WHERE column = :my_param ORDER BY :order :direction"); $stmt->bindParam(':my_param', $is_live, PDO::PARAM_STR); $stmt->bindParam(':order', $order, PDO::PARAM_STR); $stmt->bindParam(':direction', $direction, PDO::PARAM_STR); $stmt->execute(); 

The :my_param works, but not :order or :direction. Is it not being internally escaped correctly? Am I stuck inserting it directly in the SQL? Like so:

$order = 'columnName'; $direction = 'ASC';  $stmt = $db->prepare("SELECT * from table WHERE column = :my_param ORDER BY $order $direction"); 

Is there a PDO::PARAM_COLUMN_NAME constant or some equivalent?

Thanks!

like image 711
Marlorn Avatar asked Mar 30 '10 02:03

Marlorn


People also ask

Which PDO method is used to prepare a statement for execution?

Description ¶ Prepares an SQL statement to be executed by the PDOStatement::execute() method. The statement template can contain zero or more named (:name) or question mark (?) parameter markers for which real values will be substituted when the statement is executed.

How does PDO prepared statements work?

In layman's terms, PDO prepared statements work like this: Prepare an SQL query with empty values as placeholders with either a question mark or a variable name with a colon preceding it for each value. Bind values or variables to the placeholders. Execute query simultaneously.

What is the use of prepare ()?

The prepare() / mysqli_prepare() function is used to prepare an SQL statement for execution.

What is prepared statement in Mysqli?

A prepared statement is a feature used to execute the same (or similar) SQL statements repeatedly with high efficiency. Prepared statements basically work like this: Prepare: An SQL statement template is created and sent to the database. Certain values are left unspecified, called parameters (labeled "?").


2 Answers

Yes, you're stuck inserting it directly in the SQL. With some precautions, of course. Every operator/identifier must be hardcoded in your script, like this:

$orders=array("name","price","qty"); $key=array_search($_GET['sort'],$orders); $order=$orders[$key]; $query="SELECT * from table WHERE is_live = :is_live ORDER BY $order"; 

Same for the direction.

I wrote a whitelisting helper function to be used in such cases, it greatly reduces the amount of code that needs to be written:

$order = white_list($order, ["name","price","qty"], "Invalid field name"); $direction = white_list($direction, ["ASC","DESC"], "Invalid ORDER BY direction");  $sql = "SELECT field from table WHERE column = ? ORDER BY $order $direction"; $stmt = $db->prepare($sql); $stmt->execute([$is_live]); 

The idea here is to check the value and raise an error in case it is not correct.

like image 194
Your Common Sense Avatar answered Oct 09 '22 02:10

Your Common Sense


I don't think you can :

  • Use placeholders in an order by clause
  • Bind column names : you can only bind values -- or variables, and have their value injected in the prepared statement.
like image 25
Pascal MARTIN Avatar answered Oct 09 '22 03:10

Pascal MARTIN