Background
I have been in the process of developing a Content Management System from scratch. It is primarily being developed from scratch for experience reasons, but it will also be used down the road.
I developed a DatabaseManager
class which extends the mysqli
class in PHP. I am using it do MySQL queries and connections. A function I have developed passes both the SQL query string (which is parameterized) followed by an array of the correct values to be replaced.
TL;DR
Long story short, I'm using a ReflectionClass
to bind the parameters to the SQL query and execute. I am curious if this is a secure approach, or is there another method that would be best seen fit?
The Method
This is the DatabaseManager::do_query()
method:
function do_query($sql, $values){
if(!isset($this->connect_error)){
$num_vals = count($values);
$i = 0;
$type = "";
while($i < $num_vals){
if(is_int($values[$i]) == true)
$type .= "i";
elseif(is_string($values[$i]) == true)
$type .= "s";
$i++;
}
$i = 0;
while($i < $num_vals){
$values2[$i] = &$values[$i];
$i++;
}
$values2 = array_merge(array($type), $values2);
$expr = $this->prepare($sql);
if($expr != false){
$ref = new ReflectionClass('mysqli_stmt');
$method = $ref->getMethod("bind_param");
$method->invokeArgs($expr, $values2);
$expr->execute();
return "Success";
}else{
$error_string = "Error: Query preparation resulted in an error. ";
$error_string .= $this->error;
__TGErrorHandler(TG_ERROR_DB_PREP);
return $error_string;
}
}
}
I have not run into any direct errors by testing, and it seems to hold up against SQL injections, using prepared statements. Is there any underlying issues with the structure of this method, though?
P.S. I am handling SELECT
statements in a different way. This will handle DELETE
and INSERT
statements primarily.