I hear: " Just adding quotes around the contents of $ID doesn't properly convert it from arbitrary text into an SQL string literal... "

From my experience so far, "prepare" appears to do exactly that! The prepare method is much smarter than I would have thought. This is what amazed me! And I mentioned this in my post. This thing gets a string from Perl and then applies its own "rules".

How "prepare" gets the "real" value of $ID is a mystery to me. But it does! I have several programs that work with this syntax.

It does work, usually. But it is an error waiting to happen. There is no magic involved: Perl is simply interpolating the variable, and putting it's value into the location in the string.

Which sounds fine. Until someone manages to figure out how to have $ID = q[1'; DROP TABLE finalLevel3 '], in which case you are in deep trouble, because Perl will gladly send the perfectly correct SQL (well, if I've written it right...) to your database, which will nicely drop the table for you. All without any errors.

Contrast that to using placeholders: Now the value of $ID is passed as a value (not as part of the query string), and the database will get it as a chunk. You'll get an error saying q[id "1'; DROP TABLE finalLevel3 '" not found], but your database will still be intact.


In reply to Re^5: perl, mysql: "fetchrow_array failed: fetch() without execute()" by DStaal
in thread perl, mysql: "fetchrow_array failed: fetch() without execute()" by gojira

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.