By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Why can't I pass this by reference.

Samishii23
100+
P: 246
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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. $arr = array( $_FORM['data1'], $_FORM['data2'], '0', '0', ( $_FORM['data3'] ) ? $_FORM['data3'] : '0' );
  2. 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...
Expand|Select|Wrap|Line Numbers
  1. $db = new mysqli(CONFIG_DBHOST, CONFIG_DBUSER, CONFIG_DBPASS, CONFIG_DBNAME);
  2. $stmt = $db->prepare($query);
  3. $stmt->bind_param('ss',$_FORM['data1'],$_FORM['data2']); // Works
  4. $stmt->bind_param('ss','0','0'); // Does not work
Dec 30 '11 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
Edit: To give more of a quickie understanding of my lack of understanding...
Expand|Select|Wrap|Line Numbers
  1. $db = new mysqli(CONFIG_DBHOST, CONFIG_DBUSER, CONFIG_DBPASS, CONFIG_DBNAME);
  2. $stmt = $db->prepare($query);
  3. $stmt->bind_param('ss',$_FORM['data1'],$_FORM['data2']); // Works
  4. $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.
Expand|Select|Wrap|Line Numbers
  1. // awkward, but it seems like the only possibility except changing to PDO
  2. $a = 0;
  3. $b = ( $_FORM['data3'] ) ? $_FORM['data3'] : 0;
  4. $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().
Jan 1 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.