I don't know why, but no mater what I placed in the array, sets only what at the end. For example:
$mysqli->query("INSERT INTO users (username, email) VALUES (?, ?)", [$username, $email]);
Will set both the email and the usernames as $email
.
the query
function is a function I built so it'll be much easier and secure execute SQL queries. If it is a SELECT statement it'll return an array with the result.
I've checked if the generated array works good, and it is. So I believe the problem is in the call_user_func_array
, but I do not know how to fix it.
Here is the code:
public function query($query, $arr = false, $count = false){
if($arr === false){
if(!$result = $this->mysqli->query($query))
return false;
}
else
{
$types = "";
$array = [""];
foreach($arr as $value){
if(gettype($value) == "string"){
$types .= "s";
$array[] = &$value;
}else if(gettype($value) == "double"){
$types .= "d";
$array[] = &$value;
}else if(gettype($value) == "integer"){
$types .= "i";
$array[] = &$value;
}
}
$array[0] = $types;
if(substr_count($query, "?") !== count($array) - 1){
throw new exception("ERROR: The ammount of '?' in QUERY doesn't match the array");
return false;
}
if(!($result = $this->mysqli->prepare($query)))
return false;
//var_dump($result);
call_user_func_array(array($result, "bind_param"), $array);
if(!$result->execute())
return false;
}
if(strpos($query, " SELECT ") !== false){
if($count === true)
return $result->num_rows;
if($result->num_rows == 0)
return null;
if($result->num_rows == 1)
return $result->fetch_assoc();
$arr = [];
while($result->fetch_assoc())
$arr[] = $result;
return $arr;
}
return true;
}