0

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;
        }
    }
  • Here also an example with other number of params: string(94) "SELECT COUNT(*) AS number FROM tasks WHERE taskType = ? AND taskParentID = ? AND taskState = ?" string(3) "sss" array(3) { [0]=> string(4) "task" [1]=> int(6) [2]=> string(7) "finshed" } – lukaslanger Jul 12 '22 at 12:05
  • 2
    `mysqli_stmt_bind_param` isn't accepting arrays, but separate params. So you need to call the function with x params not one array with x elements. – stui Jul 12 '22 at 12:07
  • 2
    ... but you can keep the array, if you use the spread operator, https://www.php.net/manual/en/mysqli-stmt.bind-param.php#example-1477 You need to stop treating the case that there is only a single parameter differently then, even in that case you just need to pass the array then. – CBroe Jul 12 '22 at 12:10

0 Answers0