Jump to content

Prepared Statement with Dynamic # of values


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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.