Hi Monks, thanks for the prelim comments in CB, but this one seems to be more complex, or at least I'm making it more complex.

PREAMBLE

What we have is a MySQL query parser of sorts - essentially I'm abstracting the data layer of my application by storing all my queries externally to the business logic. These external queries leverage DBD::mysql's native ability to dynamically bind values to pre-made statements using the '?' feature. So I might have a simple query like "select * from clients where row_id=?" and I bind a variable eg: $user_input{row_id} to this query.

One important thing to note is that the variable names themselves are also stored externally - so nowhere do I have a line of code that says:
$sth->prepare("select * from clients where row_id=?";
$sth->execute($user_input{row_id});

but rather:
$sth->prepare(getQuery());
$sth->execute(getQueryVars());

THE ISSUE (identifier quoting in ORDER BY clause)

The issue has only come to light since we upgraded our MySQL servers to 5.x from 4.1. This method of dynamically generating queries has been operating well for nigh on 3 years. Now however, we're discovering that whenever the bind variables are being used in the context of the ORDER BY clause, the query breaks.

So: "select * from clients order by ?" and passing "last_name" won't work anymore, because (I believe) DBD::mysql adds single quotes to all its bind variables and (I believe) MySQL 5.x is now much more strict about correctly quoting identifiers (like column names).

It has been suggested that $dbh->quote_identifier() will do the trick, and this is undoubtedly so if everything were hard-coded. I'm not sure, however, that this will work, as everything is interpreted and it's not easy for my parser to tell whether the variable is used in the context of a string somewhere or whether it needs to be quoted as an identifier for an ORDER BY clause.

Any thoughts?


In reply to DBD::mysql bind variables in ORDER BY clause by dot.tom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.