I was refactoring my code from:
public function mysqliQueryPrepared($sql, $value = '', $value2 = '', $value3 = '', $value4 = '', $value5 = '')
{
$mysqli = $this->mysqliConnect();
$stmt = mysqli_stmt_init($mysqli);
if (!mysqli_stmt_prepare($stmt, $sql)) {
var_dump($sql);
$this->locationIndex("?error=sqlerror");
} else {
if ($value == '' && $value2 == '' && $value3 == '' && $value4 == '' && $value5 == '') {
} else if ($value2 == '' && $value3 == '' && $value4 == '' && $value5 == '') {
mysqli_stmt_bind_param($stmt, "s", $value);
} else if ($value3 == '' && $value4 == '' && $value5 == '') {
mysqli_stmt_bind_param($stmt, "ss", $value, $value2);
} else if ($value4 == '' && $value5 == '') {
mysqli_stmt_bind_param($stmt, "sss", $value, $value2, $value3);
} else if ($value5 == '') {
mysqli_stmt_bind_param($stmt, "ssss", $value, $value2, $value3, $value4);
} else {
mysqli_stmt_bind_param($stmt, "sssss", $value, $value2, $value3, $value4, $value5);
}
mysqli_stmt_execute($stmt);
}
}
to
public function mysqliSelectFetchObject($sql, ...$params)
{
$mysqli = $this->mysqliConnect();
$stmt = mysqli_stmt_init($mysqli);
if (!mysqli_stmt_prepare($stmt, $sql)) {
var_dump($sql);
$this->locationIndex("?error=sqlerror");
} else {
$types = str_repeat('s', count($params));
if (count($params) == 1) $params = $params[0];
var_dump($sql); // string(58) "SELECT * FROM groupaccess WHERE userID = ? AND groupID = ?"
var_dump($types); // string(1) "ss"
var_dump($params); // array(2) { [0]=> int(4) [1]=> int(66) }
mysqli_stmt_bind_param($stmt, $types, $params);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if ($result) {
return mysqli_fetch_object($result);
}
}
}
However I am getting the warning that the number of arguments is not matching and I can't explain it to myself, because you can clearly see in the var_dump()'s that everything is matching. But what is most confusing that my similar function is working perfectly without errors or warnings.
public function mysqliSelectFetchArray($sql, ...$params)
{
$mysqli = $this->mysqliConnect();
$stmt = mysqli_stmt_init($mysqli);
if (!mysqli_stmt_prepare($stmt, $sql)) {
var_dump($sql);
$this->locationIndex("?error=sqlerror");
} else {
$types = str_repeat('s', count($params));
if (count($params) == 1) $params = $params[0];
mysqli_stmt_bind_param($stmt, $types, $params);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if ($result) {
while ($obj = mysqli_fetch_object($result)) {
$data[] = $obj;
}
}
return $data;
}
}