Question: Im new to database and i have written a LOT of PHP code that accesses a database using MySQL. 

I didnt take into account SQL injection attacks so i have to re-write all that PHP code to use mysql prepared statements.

After looking at videos on how to used prepared SQL statements, to perform just ONE SQL command requires a whole lot of "prepared" statements. My existing code has lots of different SQL statements all over the place, it would be a nightmare to change all that code to pack and unpack all the required preparation for each "prepared" statement command.

Is there some kind of wrapper i can use to prevent turning one line of regular SQL into 6 or 7 lines of prepared statements?

For example use to do this line line of SQL

```
SELECT * from users where userid=10

```

needs many more lines of prepared SQL statements, especially if there are lots of other SQL statements too it now becomes very complex.

Is there was some sort of one line wrapper that i can call that accepts the template SQL string, plus the parameters, which also executes the command and returns the result in just one line of wrapper for different types of MYSQL statements it would be great and the code would be much less confusing looking and error prone.

For example

```
 $users=WrapAndExecute($db,"SELECT * from users where userid=?","s",$userid);

 $data=WrapAndExecute($db,"UPDATE table SET username=?,city=?","ss",$name,$city);

 $result=WrapAndExecute($db,"DELETE from table where id=?","s",$userid);

 $result=WrapAndExecute($db,"INSERT into ? (name,address) VALUES(?,?)","ss","users",$name,$address);

```

Each of those lines above would create a prepared statement template, do the bind, execute it and return the result that a regular MYSQL statement would. This would create minimal impact on existing code.

Anybody knows how to do this or if some easy php library or class already exists to do this, that i can just import and start using it?

Thanks

Answer: 
I were in the same boat, and I wrote [such a wrapper](https://github.com/colshrapnel/safemysql) that works exactly the way you want, save for it's being a class, not a function.

```
$user = $sdb->getRow("SELECT * from users where userid=?s", $userid);
$sdb->query("UPDATE table SET username=?s, city=?s", $name, $city);
$sdb->query("DELETE from table where id=?s", $userid);
$sdb->query("INSERT into ?n (name,address) VALUES(?s,?s)","users", $name, $address);

```

The above is a *working code*, as long as you have somewhere in your bootstrap file

```
$db = mysqli_connect(...);
...
require 'safemysql.class.php';
$sdb = new SafeMySQL('mysqli' => $db);

```

Note that none of the other suggestions could do anything like that.

Also note that if I were writing it today, I would have used PDO, as this class is duplicating a lot of functionality already exists in PDO.
Since a key requirement seems to be that you can implement this with minimal impact on your current codebase, it would have been helpful if you had told us what interface you currently use for running your queries.

While you could use PDO:

* that means an awful lot of work if you are not already using PDO
* PDO exceptions are **horrible**

Assuming you are using procedural mysqli (and have a good reason not to use [mysqli\_prepare](http://php.net/manual/en/mysqli.prepare.php)()) its not that hard to write something (not tested!):

```
function wrapAndExecute()
{
   $args=func_get_args();
   $db=array_shift($args);
   $stmt=array_shift($args);
   $stmt_parts=explode('?', $stmt);
   if (count($args)+1!=count($stmt_parts)) {
        trigger_error("Argument count does not match placeholder count");
        return false;
   }
   $real_statement=array_shift($stmt_parts);
   foreach ($args as $k=>$val) {
      if (isnull($val)) {
         $val='NULL';
      } else if (!is_numeric($val)) {
         $val="'" . mysqli_real_escape_string($db, $val) . "'";
      }
      $real_statement.=$val . array_shift($stmt_parts);
   }
   return mysqli_query($db, $real_statement);
}

```

Note that this does not handle `IS [NOT] NULL` nicely nor a literal '?' in the statement nor booleans (but these are trivial to fix).