Bluehost.com Web Hosting $6.95

PDO and "ORDER BY" clausel with parameters

This is a discussion on PDO and "ORDER BY" clausel with parameters within the PHP General forums, part of the PHP Programming Forums category; Hello! I would like to know how I should bind a value to a parameter in a "ORDER BY&...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-07-2008
igor.sverkos@googlemail.com
 
Posts: n/a
Default PDO and "ORDER BY" clausel with parameters

Hello!

I would like to know how I should bind a value to a parameter in a
"ORDER BY" clauses (the safe way).
I tried:

$sortfield = 'timestamp';
$sortorder = 'DESC';
$sql = "
SELECT newsid, title, timestamp
FROM news
ORDER BY :sortfield :sortorder
LIMIT 10
";
$newsitems = $dbh->prepare($sql);
$newsitems->bindparam(':sortfield', $sortfield, PDO::PARAM_STR);
$newsitems->bindparam(':sortorder', $sortorder, PDO::PARAM_STR);
$newsitems->execute();
[...]

But this will fail, because the query will look like

SELECT newsid, title, timestamp
FROM news
ORDER BY 'timestamp' 'DESC'
LIMIT 10

Is there a way to do it with PDO (I like the way PDO will secure the
values) or do I need to do it by myself?


Thanks!

--
Igor
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:15 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0