Sample class to escape a MySQL input value
There was quite a simple but effective snippet I came across recently
that highlights how PHP getters (magic methods) can be useful.
Please don’t use this code as it is, as it’s for illustrative purposes
only; it doesn’t check for magic quotes that the server may be using,
and you may want to do additional purification of the data according
to the field’s content. However, I do think it’s a useful example of
how classes can be used effectively, and this could be expanded, in
theory, to add a static type for input type, that’s initialised during
__construct(), which could make it a little more dynamic.
class dbQuoteStr
{
function __get($value)
{
return mysql_real_escape_string($value);
}
}
$dbQuoteStr = new dbQuoteStr();
To use the above class, all you need to do is:
mysql_query("SELECT * FROM users WHERE name LIKE
'{$dbQuoteStr->$name}' LIMIT 10");
Categories
Japanese Word of the Day
Lang-8 Journal- muso : 面白いサイン (15) May 15, 2012
- muso : 一番好きな漢字 (9) May 13, 2012
- muso : 猫の餌をやるな (9) May 12, 2012
- muso : テレビのプログラム (15) May 8, 2012
- muso : 法定休日 (9) May 7, 2012
- muso : お腹が空いた (9) May 6, 2012
- muso : 漢字の練習日本語のレッスンします (11) April 29, 2012
- muso : 文楽 (4) April 24, 2012
- muso : 日本から帰りました。 (10) April 16, 2012
- muso : 五月蝿い猫です。 (9) March 22, 2012






Recent Comments