Store references in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wisni1rr
    New Member
    • Nov 2011
    • 78

    Store references in array

    I'm writing a dynamic search page for my mysql database.

    I'm having trouble with my mysqli_stmt_bin d_param() statement entering multiple parameters.

    Before the statement I am saving all my parameters in to an array called $paramArray.

    How would I add each element of the $paramArray to end of the function arguments?

    My original thought was to implode the array with the "," delimiter. However, the function will treat the implode as a single string.

    Code:
    //Excerpt
    $paramArray[] = 100;
    $paramArray[] = 500;
    $paramArray[] = 800;
    
    ...
    
    mysqli_stmt_bind_param($stmt, "iii", implode(",",$paramArray));
    The implode function creates the string, "100,500,80 0".
    However I need it to work like this:

    Code:
    mysqli_stmt_bind_param($stmt, "iii", $paramArray[0], $paramArray[1], $paramArray[2]);
    Any ideas? Thanks in advance!
  • Exequiel
    Contributor
    • Jul 2012
    • 288

    #2
    can you state some output examples that you want to print by your code??

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      My original thought was to implode the array with the "," delimiter. However, the function will treat the implode as a single string.
      because it is a single string.

      Before the statement I am saving all my parameters in to an array called $paramArray.

      How would I add each element of the $paramArray to end of the function arguments?
      unfortunately there is no easy way to do that in mysqli. you either do it manually (as demonstrated in your last code block) or you could try call_user_func_ array():
      Code:
      array_unshift($paramArray, str_pad("", "i", count($paramArray)));
      call_user_func_array(array($stmt, "bind_param"), $paramArray);

      Comment

      • wisni1rr
        New Member
        • Nov 2011
        • 78

        #4
        Thanks.

        In order to get this to work I needed to add "&" to each of my variables stored in my $paramArray. (They are not all static values.)

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          (They are not all static values.)
          you must not pass values into ->bind_param() only variables, hence you need the references.

          Comment

          Working...