2

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;
    }
  • you have to do it by using bind_parm of mysqli. directly giving it into query is not a good idea. – Alive to die - Anant May 10 '15 at 21:37
  • my guess is because you are using `$array[] = &$value;`, so the last `$value` value, which in your case is `$email`, will overwrite all other `$value` values. – Sean May 10 '15 at 21:46
  • @Sean If I'll delete it, it'll show error `Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given`. –  May 10 '15 at 21:51
  • possible duplicate of http://stackoverflow.com/questions/16120822/mysqli-bind-param-expected-to-be-a-reference-value-given – Sean May 10 '15 at 21:55
  • why are you calling `bind_param` by `call_user_func_array`, what about `$result->bind_param`? – Axel Amthor May 10 '15 at 21:56
  • @Sean How is it a duplicate? The questions have nothing in common expect they both about bind_param –  May 10 '15 at 21:59
  • @AxelAmthor as I've said it, to make it much easier. This makes a 5-6 lines of code to one line. (no need to use prepare, generate the first string in the array, get the resoults and set them into array etc. All of that in one line. –  May 10 '15 at 22:01
  • that is why I did not do a close vote, only as a comment. It was more to look at the way selected answer http://stackoverflow.com/a/16120923/689579 was doing the `bind_param()` in conjunction with `call_user_func_array()`. – Sean May 10 '15 at 22:01
  • @Sean the part of the `call_user_func_array` in my code is same as there, except he uses function and I'm setting it to reference in the first lines. The problem is not that it doesn't work, the problem is that it sets all the `?` in the query as the last parameter in the array. –  May 10 '15 at 22:04
  • As Sean has said, there is only one variable in the `foreach` loop: `$value` that you are binding with. Use the reference in the `foreach` loop and it should work: `foreach($arr as &$value)`. You still need the `$array[] = &$value;` statement, – Ryan Vincent May 11 '15 at 03:28

0 Answers0