A good way to do this is to implement the Wrapper class to use mysql_ * functions with several methods to create prepared statements.
The idea is that you must pass strongly typed parameters in your queries.
For example, here is a piece of code with a general idea. Of course, this requires more work. But this can interfere with SQL Injection attacks if they are implemented.
You can also look for third-party libraries that already implement this, because this is a common occurrence.
<?php class MyDb { protected $query; public function setQuery($query) { $this->query = $query; } public function setNumericParameter($name, $value) { if (is_numeric($value)) // SQL Injection check, is the value really an Int ? { $this->query = str_replace(':'.$name, $value); } // else, probably an intent of SQL Injection } // Implement here the methods for all the types you need, including dates, strings, etc public function fetchArray() { $res = mysql_query($this->query); return mysql_fetch_array($res); } } MyDb $db = new MyDb(); $db->setQuery('SELECT * FROM articles WHERE id = :id'); $db->setNumericParameter('id', 15); while ($row = $db->fetchArray()) { // do your homework }
source share