Even if that does protect you, it also destroys perfectly valid input.
Your input is either going to be a string or a non-string value that matches a regular expression. For example, integers should always match /[+-]\d+/. PHP probably has what you need built in.
Strings are easy to escape in SQL. I'm not a PHP guy, but it's as simple as:
Code:
"'" . str_replace("'", "''", $input) . "'"
Now you've got a quoted string. But don't take my word for it,
read the spec.
Code:
<character string literal> ::=
[ <introducer> <character set specification> ] <quote> [ <character representation> ... ] <quote> [ { <separator> ... <quote> [ <character representation> ... ] <quote> }... ]
<quote> ::= '
<character representation> ::= <nonquote character> | <quote symbol>
<nonquote characte r> ::= !! See the Syntax rules
<quote symbol> ::= <quote> <quote>
<separator> ::= { <comment> | <space> | <newline> }...
Syntax Rules
1) In a <character string literal> or <national character string
literal>, the sequence:
<quote> <character representation>... <quote>
<separator>... <quote> <character representation>... <quote>
is equivalent to the sequence
<quote> <character representation>... <character representa-
tion>... <quote>
Note: The <character representation>s in the equivalent se-
quence are in the same sequence and relative sequence as in the
original <character string literal>.
Basically, it's saying two things: you can represent a quote with two quotes. (That's the quote symbol rule.) And that if you have two strings next to each other, like 'foo' 'bar' it's the same as saying 'foobar'. This is the way it works in C and other languages, too. (Well, there's more stuff about choosing character sets, but I can't imagine that would work very well...)
Also note that there's no reason that 'a string on two lines
doesn''t work just fine'. And having keywords inside the quote marks doesn't bother SQL in the least... so long as it's properly quoted, it gets turned into a character string literal token by the
lexer.