Re: PDO + ORDER BY with prepared statements!



On Thu, 28 Feb 2008 13:06:39 +0100, João Morais <jcsmorais@xxxxxxxxx> wrote:

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?

AFAIK, you cannot do this (but to be honest, haven't really tried, bur there's no such thing like a PDO::PARAM_COLUMN_NAME).

The reason your result is not sorted, but has no error, is this, the resulting query is:
SELECT field1, field2, field3
FROM table
ORDER BY 'field1';
and NOT:
SELECT field1, field2, field3
FROM table
ORDER BY field1;
.... so the query isn't sorted by a field, but rather by a fixed string, which would result in all rows being exactly equal in the sort order, so the database will just send the rows as they appear in its storage.

In this case, contrary to unpredictable user supplied data, a columnname to sort by is easily whitelisted, so I'd use simple string building to get a correct statement.
--
Rik Wasmus
.



Relevant Pages

  • Re: Is it possible to link two text boxes...
    ... The only thing I can think of would be to divide the text in the query. ... SELECT MyTextField AS Field1, MyTextField AS Field2 ... question is that I have two images on the page and one is justified left ...
    (microsoft.public.access.reports)
  • Re: Append import data to existing record
    ... (I'm sure I need to exchange Field1 with my actual field name but what about ... > or query with values from a non-updatable table or query - even though ... > in the text file - is to write VBA code that uses recordset operations ... > to read the data from the text file and update the subform table. ...
    (microsoft.public.access.externaldata)
  • Re: Group By, Max and Min
    ... SELECT field1, field2, field3, LAST, MIN, MAX ... I have (from a previous saved query) these fields: ...
    (microsoft.public.access.queries)
  • Re: query or script
    ... I want to select the whole record if the value in field1 is ... selects only the two important fields, with the distinct property ... Convert that into an Append query, ... If you need one value from field2, we'll need to know how to ...
    (comp.databases.ms-access)
  • Re: Create Weekly and Monthly Flag in database/table
    ... WHERE field1 In; ... the weekday(datefield [,optional first day of week)) returns a ... In the query designer if you put your field2, ... small table with the holidays that fall on a friday. ...
    (comp.databases.ms-access)