View Single Post

  #1 (permalink)  
Old 02-28-2008
Joćo Morais
 
Posts: n/a
Default PDO + ORDER BY with prepared statements!

Hi there guys,

I've been working with PDO for a couple of days now.. and now I got
into this problem:

$order_by = 'field1';
$order_type = 'DESC';

$query = 'SELECT field1, field2, field3 ';
$query .= 'FROM table ';
$query .= 'ORDER BY :order_by :order_type ';

$stmt = $dbh->prepare($query);
$stmt->bindParam(':order_by', $order_by, PDO::PARAM_STR);
$stmt->bindParam(':order_type', $order_type, PDO::PARAM_STR);
$stmt->execute();

while($row = $stmt->fetch(PDO::FETCH_NUM))
method_to_display($row);


When statement gets executed it returns true, although while I'm
listing query results they aren't sorted by field1 with specified
order_type.

Can any one explain what I'm doing wrong?

Thanks in advance.

Reply With Quote