PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > PDO + ORDER BY with prepared statements!
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
PDO + ORDER BY with prepared statements!

Réponse
 
LinkBack Outils de la discussion
Vieux 28/02/2008, 12h06   #1
João Morais
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 rder_by rder_type ';

$stmt = $dbh->prepare($query);
$stmt->bindParam('rder_by', $order_by, PDO::PARAM_STR);
$stmt->bindParam('rder_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.

  Réponse avec citation
Vieux 28/02/2008, 12h45   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: PDO + ORDER BY with prepared statements!

On Thu, 28 Feb 2008 13:06:39 +0100, João Morais <jcsmorais@gmail.com>
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 rder_by rder_type ';
>
> $stmt = $dbh->prepare($query);
> $stmt->bindParam('rder_by', $order_by, PDO::PARAM_STR);
> $stmt->bindParam('rder_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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h08.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11987 seconds with 10 queries