Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^5: Getting mysql data into gd

by roboticus (Chancellor)
on Sep 04, 2017 at 00:02 UTC ( [id://1198641]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Getting mysql data into gd
in thread Getting mysql data into gd

shanta:

DBI has all the SQL quoting rules handled, so using placeholders would turn it into:

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

Here, the drop table command isn't exposed as an SQL statement, it's just a column value checked in the where clause. If you were really careful all the time and made sure you handled all the quoting and comments properly, you could ignore placeholders and not run into trouble. But I don't know anyone careful enough to *always* get it right. Instead DBI does it for you, and you don't need to remember the rules for whichever database you use.

Some database back ends understand placeholders and can give you a performance boost when you use them, so using placeholders gives you the chance to squeeze a little more performance out of your database for the ones that understand it (such as Sybase, MSSQL, Oracle, Pg).

For example, suppose you wanted to do something like this:

insert into my_table select ? as foo, ? as bar, c.id as product_id, d.price as product_pric +e from my_stuff c where c.name=? join friend_prices d where d.name=? and c.class=d.class

When the SQL back-end parses this, it writes a little program to do the table joins and lookups for the insert. If you just substitute the values into the SQL and prepare then execute it each time, it has to repeat all the work to generate the query plan each time. But if you use placeholders, it only needs to do generate the query plan once, because you can re-use the statement:

my $ST = db->prepare(q{ insert into my_table select ? as foo, ? as bar, c.id as product_id, d.price as product_ +price from my_stuff c where c.name=? join friend_prices d where d.name=? and c.class=d.class }); $ST->execute(123, 456, 'Mouse', 'Joe'); $ST->execute(149, 357, 'Mousepad', 'Joe');

...roboticus

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1198641]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-25 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found