I have inherited some PHP code that is not ideal and I'm not sure what the most efficient way of rectifying it is.
Basically all DB calls are made through a custom function like this:
function dbcommand($req)
{
global $mysqli;
$backtrace = debug_backtrace();
$ret = array();
$res = mysqli_query($mysqli, $req) or die('SQL Query Error: '. $mysqli->error .', query ['. $req .'] at '. $backtrace[0]['file'] .':'. $backtrace[0]['line']);
if (strpos(strtoupper($req), 'SELECT') === 0)
{
if (mysqli_num_rows($res))
{
while ($row = mysqli_fetch_assoc($res))
$ret[] = $row;
}
else $ret = array();
mysqli_free_result($res);
return $ret;
}
if (strpos($req, 'INSERT INTO') === 0)
return $mysqli->insert_id;
return $res;
}
Now I don't think I can use mysqli_real_escape_string because of the db-connector issue. Everything goes through that function. This means that avoiding sql injection is left in the hands of filter_vars before variables are mulched into SQL statements. I would like to parameterise my SQL statements and do it properly. But I'm just not sure what the most efficient way of doing it is in this case. Removing this function and converting everything to PDO would be very time consuming. There's a lot of code.
debug_backtrace()every time, even thought you only need it in the rare failure case, is likely to be expensive.