0

I know they're many, many questions already about this, but I have no idea why mine doesn't work.

If I just use the following, my code works fine: $this->stmt->bind_param("ii", $params[0], $params[1]);

But if I use the call_user_func_array, it breaks. One suggestion I got was passing the $parameters array by reference, but adding an & before the variable also broke my code...

Any help is greatly recieved!

Here's my code:

DB class:

function selectQuery($sql, $paramTypes = false, $params = false) {
        //Prepare statement
        $this->stmt = $this->conn->prepare($sql);

        if($this->stmt === false) {
            //We have an error
            echo 'Wrong SQL: ' . $sql . ' Error: ' . $this->conn->error;
        }

        //This part doesn't work...

        // if ($params) {
        //  //Bind an unknown number of parameters
        //  $parameters = array_merge(array($paramTypes), $params);

        //  call_user_func_array(array($this->stmt, 'bind_param'), $parameters);
        // }

        //This works.

        $this->stmt->bind_param("ii", $params[0], $params[1]);

        //Execute statement
        $this->stmt->execute();

        if ($this->stmt->error) {
            echo $this->stmt->error;
            return false;
        }

        //Get the results
        $result = $this->stmt->get_result();

        $data = $result->fetch_all(MYSQLI_ASSOC);

        //Close statement
        $this->stmt->close();

        //Return the results
        return $data;
    }

Test page:

<?php
require_once('DatabaseAccess.php');

$db = new DB();

$sql = "SELECT * FROM table WHERE id = ? OR id = ?";

echo "Fetching data....<br>";
$result = $db->selectQuery($sql, "ii", array(1, 2));

foreach($result as $r) {
    echo "<pre>".print_r($r, 1)."</pre>";
}
?>

Decided to add more information:

I'll be using this function to pass in the parameter types and parameters, but the amount will vary. When I looked up how to do this everyone suggested the call_user_func thing, but each time I try it (tried a few different ways) it won't work. Read through many threads, but it never seems to work. If I just use the bind_params function directly it works and I get the correct data returned.

Using the call_user_func thing I was getting the no data for the ? mysqli error, which is when I tried passing by reference and the code just broke completely...

5
  • How you use the call_user_func_array() and how you pass params into this function? Commented Dec 7, 2015 at 15:11
  • The params are passed from the test page, code is above. The call_user_func_array() is also in the code above, in the DB Class section (it's commented out) Commented Dec 7, 2015 at 15:13
  • 1
    you cant pass parameters by reference - that's written in documentation; so try this. mb this will help you $res = array(); foreach($parameters as $key => $value) {$res[$key] = &$parameters[$key];} Commented Dec 7, 2015 at 15:20
  • Could you expand on that a little in an answer? I'm really not understanding all of this at the moment :( Commented Dec 7, 2015 at 15:26
  • I've got it working using you're code. Thank you! If you put it in an answer I can mark it as correct :) Commented Dec 7, 2015 at 15:49

1 Answer 1

1

Put it before call_user_func_array()

$res = array();
foreach($parameters as $key => $value) {
    $res[$key] = &$parameters[$key];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.