|There's more than one way to do things|
Re^3: Getting mysql data into gdby roboticus (Chancellor)
|on Sep 03, 2017 at 19:18 UTC||Need Help??|
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:
As mentioned, you change the variables to question marks and provide the variables to the execute statement, like this:
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):
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.