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&...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |