Jump to content

Prepared Statement with Dynamic # of values


BuildMyWeb
Go to solution Solved by BuildMyWeb,

Recommended Posts

 i cannot seem to figure out how to pass my array of values here. i get the following error(s):

Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in ... on line 54



Fatal error: Uncaught exception 'ReflectionException' with message 'Invocation of method mysqli_stmt::bind_param() failed' in ... ReflectionMethod->invokeArgs(Object(mysqli_stmt), Array) #1 {main} thrown in ...

    $action            = clean_string( $_POST['action'] );

    $user_select    = $_POST['user_select'];



    if( empty($action) )

    { header('Location: ' .  C_REF . '?msg_raw=act'); exit(); }



    else

    {

        $in                = '?';

        $types             = 'si';

        $arr_values        = array($action, $user_select[0]);

        

        for( $i=2; $i < count($user_select)+1; $i++ )

        {

            $in              .= ', ?';

            $types            .= 'i';

            $arr_values[]   = $user_select[$i-1];

        }

        

        array_unshift($arr_values, $types);



        $sql = "UPDATE users SET status = ? WHERE id IN (" . $in . ")";

        $stmt = $db_connect->prepare($sql);

        $ref    = new ReflectionClass('mysqli_stmt');

        $method = $ref->getMethod("bind_param");

        $method->invokeArgs($stmt,$arr_values);

        $stmt->execute();  

        

    } // close else
Link to comment
Share on other sites

  • Solution

ive been working on this off and on for two days.  finally figured it out.  for anyone who references this thread, here is the solution:

function refValues($arr){
            if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
            {
                $refs = array();
                foreach($arr as $key => $value)
                    $refs[$key] = &$arr[$key];
                return $refs;
            }
            return $arr;
        }
        
        $arr_values = refValues($arr_values);

http://stackoverflow.com/questions/16120822/mysqli-bind-param-expected-to-be-a-reference-value-given

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.