in reply to Getting mysql data into gd

Side point: Using a variable inside a sql statement is known as dynamic sql, and is generally considered to be a bad idea. Instead, use a place holder, that is '?', and prepare the statement.

Replies are listed 'Best First'.
Re^2: Getting mysql data into gd
by shanta (Novice) on Sep 03, 2017 at 16:53 UTC

    I am a hacker using Perl to get a job done. Entirely self taught. Perl Arrays and esoteric place holders have always mystified me!

    Heck I can do a TTML quires and populate a table or whatever but Don't know how to access GD from TTML or I would rather than drop into Perl.

    I can take code and make it do what I want. To a point at least.

    Of course I am here to learn so can you demystify it for me?


      Placeholders are actually very easy, and prevent a lot of problems. To use placeholders, you just replace variable references with question marks, then provide the values as arguments to execute.

      So suppose you have something like this:

      my $ST = $DB->prepare("select foo, bar from my_table " ."where col1 < '$abc' or (col1 > '$def' and col3 +< $fgh)"); $ST->execute();

      As mentioned, you change the variables to question marks and provide the variables to the execute statement, like this:

      my $ST = $DB->prepare("select foo, bar from table where col1 < ? or (c +ol1 > ? and col3 < ?)"); $ST->execute($abc, $def, $fgh);

      Note that we also removed the quotes on the first two variable references, because the database already knows that column1 is a string.

      Using placeholders is nice, because you can use the same statement with different values for different executes which will let the database compile the statement once and re-use the same execution plan for successive runs. (Well, at least *some* databases will take advantage of that.)

      Placeholders are even more important because they take advantage of any funky special cases for quoting the variable values, and thereby help you resist SQL injection attacks. For example, suppose $abc in the example above contains the value "'; drop table my_table; --". Then your original statement would be passed to the database as the following (with a few newlines thrown in for readability):

      select foo, bar from my_table where col1 < ''; drop table my_table ; --' or (col1 > 'def_val' and col3 < fgh_val)

      That's a recipe for disaster. So make the effort to learn placeholders a bit better (shouldn't take long), and you'll never go back to doing it the hard way.


      When your only tool is a hammer, all problems look like your thumb.

        thanks for the great response

        I can see the drift of it. How would this prevent drop table my_table example? It could be in the variable being sent anyway.

        can I send?

        $ST->execute(text, text2, moretext);

        Thanks for the help