Why can't I pass this by reference.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Samishii23
    New Member
    • Sep 2009
    • 246

    Why can't I pass this by reference.

    So I had built a dynamic MySQLi query class to handle all of my querys, and it was reuseable. Anyways. How I do it, is using this:
    Code:
    call_user_func_array( array( $stmt, 'bind_param' ), vref($arr) );
    vref() being a function that passes all that variables through references.

    So I used the above to do something like this:
    Code:
    $arr = array( $_FORM['data1'], $_FORM['data2'], '0', '0', ( $_FORM['data3'] ) ? $_FORM['data3'] : '0' );
    call_user_func_array( array( $stmt, 'bind_param' ), vref($arr) );
    And I kept getting the error, "incorrect number of binds to variables." So I checked, double checked, triple, checked... And so forth. Until this morning when I wrote out the MySQLi by hand, and manually entered each value into the $stmt->bind_param(). Then I got the error, "Could not pass parameter 3 as reference." Ok, so I need to actually pass a variable to the bind param, ok I'll change that.

    But whats confusing is this: I pass all the values through the array. Then submit that array to the call_user_func_ array() but that array apparently needs to have variables in it for reference passing as well? I don't get this. Is this because of the 5.3 reference passing thing anyways? ( I don't know what changed in 5.3 to make this a nessecary change in the first place )

    Edit: To give more of a quickie understanding of my lack of understanding.. .
    Code:
    $db = new mysqli(CONFIG_DBHOST, CONFIG_DBUSER, CONFIG_DBPASS, CONFIG_DBNAME);
    $stmt = $db->prepare($query);
    $stmt->bind_param('ss',$_FORM['data1'],$_FORM['data2']); // Works
    $stmt->bind_param('ss','0','0'); // Does not work
    Last edited by Samishii23; Dec 30 '11, 11:06 PM. Reason: Clarification
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Edit: To give more of a quickie understanding of my lack of understanding.. .
    Code:
    $db = new mysqli(CONFIG_DBHOST, CONFIG_DBUSER, CONFIG_DBPASS, CONFIG_DBNAME);
    $stmt = $db->prepare($query);
    $stmt->bind_param('ss',$_FORM['data1'],$_FORM['data2']); // Works
    $stmt->bind_param('ss','0','0'); // Does not work
    that is why it doesn’t work. bind_param() expects variables to be given, because you cannot point to a value (resp. a memory block) in PHP (cf. function arguments). for a quick fix, you would have to use variables in your array and assign them somewhere else.
    Code:
    // awkward, but it seems like the only possibility except changing to PDO
    $a = 0;
    $b = ( $_FORM['data3'] ) ? $_FORM['data3'] : 0;
    $arr = array($_FORM['data1'], $_FORM['data1'], $a, $a, $b);
    personally, that is one reason for me to use PDO, since you can choose between bindParam() and bindValue().

    Comment

    Working...