If you’ve ever used PHP, you’ve probably used, or at least seen, the PRINTF and/or SPRINTF functions. These oldie, but goodie, functions are stolen… err… borrowed from the old C language world. Both functions support the abililty to have placeholders, which when evaluated, are replaced with the value in a comma-delimited list in the appropriate position. For example:
1 | printf("My name is %s. I am %i years old", $name, $age); |
This code, given that $name equals “Adam”, and $age equals “30″ will output “My name is Adam. I am 30 years old”. A nifty feature.
Below is a bit of code that provides a class that will take a given input SQL string, an array of arguments, and spit out a parsed SQL string. An example of usage would look like so:
1 2 3 | $userId = 30; $parser = new SqlScriptParser("SELECT name, email, active FROM users WHERE userId=%i", array($userId)); $result = $parser->Parse(); |
The resulting string would look like:
SELECT name, email, active FROM users WHERE userId=30
The code: http://www.adampresley.com/downloads/code/php/sqlScriptParser.zip
Related Posts
No related posts.


[...] – Parsing PHP-style Placeholders for SQL Scripts saved by jasrajs2008-09-17 – John Edwards: Lie. Deny. Stonewall. Sidestep. Repeat. Repent. saved by [...]