Pass by reference problem with PHP 5.3.1 [duplicate] - reference

This question already has answers here:
mysqli bind_param() expected to be a reference, value given
(3 answers)
Closed 12 months ago.
Ok, this is a weird problem, so please bear with me as I explain.
We upgraded our dev servers from PHP 5.2.5 to 5.3.1.
Loading up our code after the switch, we start getting errors like:
Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in /home/spot/trunk/system/core/Database.class.php on line 105
the line mentioned (105) is as follows:
call_user_func_array(Array($stmt, 'bind_param'), $passArray);
we changed the line to the following:
call_user_func_array(Array($stmt, 'bind_param'), &$passArray);
at this point (because allow_call_time_pass_reference) is turned off, php throws this:
Deprecated: Call-time pass-by-reference has been deprecated in /home/spot/trunk/system/core/Database.class.php on line 105
After trying to fix this for some time, I broke down and set allow_call_time_pass_reference to on.
That got rid of the Deprecated warning, but now the Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference warning is throwing every time, with or without the referencing.
I have zero clue how to fix this. If the target method was my own, I would just reference the incoming vars in the func declaration, but it's a (relatively) native method (mysqli).
Has anyone experienced this? How can I get around it?
Thank you.

I just experienced this same problem, calling bind_param via call_user_func_array and passing an array of parameters. The solution is to modify the values in the array to be referenced. It's not elegant but it works.
call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced($passArray));
function makeValuesReferenced($arr){
$refs = array();
foreach($arr as $key => $value)
$refs[$key] = &$arr[$key];
return $refs;
}

You are passing an array of elements ($passArray). The second item inside the passed array needs to be a reference, since that is really the list of items you are passing to the function.

Actually, be aware that there is a bug with PHP 5.3.1 concerning references and all call family of functions:
PHP Bugs #50394: Reference argument converted to value in __call
The behavior you are seeing might be a result of this bug and any attempt to fix it code wise may cause problems in the long run.
The problem has been fixed in the SVN version of PHP. Until 5.3.2 is released, you may compile a new version for use, or downgrade to an earlier version.

We were experiencing this same problem with this code:
call_user_func(array($strCartHandler, 'CartPurchaseEvent'), $strCartEvent, $objToUser, null, $this);
My solution was to just skip call_user_func altogether and do this:
$strCartHandler::CartPurchaseEvent($strCartEvent, $objToUser, null, $this);

I think what is deprecated is passing a reference through a function. In the function definition you do something like:
function(&$arg) {
}
This doesn't help you much but you probably need not pass the reference anyway. I guess you could try a wrapper function.
function wrapper($stmt, &$passArray) {
call_user_func_array($stmt, $passArray);
}

I think the mysqli_bind_param() and mysqli_bind_result() functions are very awkward to use. I've encountered the same difficulty as you describe using them in combination with call_user_func_array()
My workaround was to stop using mysqli and instead use PDO_mysql. It has a much easier usage:
$pdoStmt->execute( $passArray );

This will helps:
<?php
call_user_func_array(Array($stmt, 'bind_param'), array(&$passArray));
function bind_param ($val)
{
$val = (is_array($val)) ? $val[0] : $val;
// operations...
}
?>

The second paramer Must be an array. apparently this was only enforced in 5.3

I've got a similar problem, the current code didnt work:
$query="Select id,name FROM mytable LIMIT ?,?";
$params=Array('ii');
array_push($params,$from_var);
array_push($params,$to_var);
...
$stmt=$link->prepare("$query");
$ref=new ReflectionClass('mysqli_stmt');
$method=$ref->getMethod("bind_param");
$method->invokeArgs($stmt,$params);
...
It told that "Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given"
And then, in despair, I've tried to take $from_var and $to_var in quotes. And it worked!
$params=Array('ii');
array_push($params,"$from_var");
array_push($params,"$to_var");
Hope, it will help somebody, good luck :)

Related

Property assignment expected. This expression is not callable. Type {} has no call signatures. (how to solve this?)

I wanted to update a badge count in a chat room using cloud function trigger which is a nested map.
I'm updating badges in the frontend without any issue but I want the create a cloud function for this to reduce frontend workloads.
but when I tried to pass a dynamic variable as field/key, the compiler is not happy and it's giving me an error.
the expected output should be like this
Backtick doesn't work so I tried to change the backtick to double quote to be able to compile, but this was the output which is not what i want.
anyone knows how to make this work? please help.
its syntax issue, missing [ & ].
it should be { [`badgeCount.${receveid}`]: fire.....

Matlab function defintion method that takes string as the definition (MFILE)

there is this method I used the other day and I have forgotten the details, which in we used a syntax like this:
f=//command//(x,'sin(x)');
something like this.
im not sure if the syntax is fully correct, or what the right command is. but after this we could simply ask for the f(x) value like this:
x= 0;
y= f(x);
and then the results were y=0;
What you are asking for is usually not recommendable. Please check if a simple anonymous function also fits your requirements:
f=#(x)(sin(x))
In case you really need to evaluate from a string:
f=str2func('#(x)sin(x)')
I would advice against the second option unless absolutely required, it can lead to hard to debug errors.
well I found the answer myself and it was "inline" command; :)
f=inline('sin(x+y+z)','x','y','z');
you can add as much variables as needed too.

How can to use fn:contains case-insensitive in JSL EL?

I trying to find out if one string contains another. But, unfortunately, fn:contains function is case-sensitive. Are there are any ways to make it case-insensitive?
I tried to put both into one case:
fn:contains(car.color.toLowerCase(), smartBean.txt.toLowerCase()) ? 'true' : 'false'
But it didn't work due to method's brackets. I also can't use f:to-upper inside f:contains function.
There's a fn:containsIgnoreCase(). Just use it instead.
#{fn:containsIgnoreCase(car.color, smartBean.txt)}
By the way, your failed toLowerCase() attempt should have been done as follows:
#{fn:contains(fn:toLowerCase(car.color), fn:toLowerCase(smartBean.txt))}
Using toUpperCase() works as good as well:
#{fn:contains(fn:toUpperCase(car.color), fn:toUpperCase(smartBean.txt))}
Perhaps you just made an EL syntax error.
Note that the ? 'true' : 'false' part is completely superfluous as that's already returned by the function.
For those just finding this, #BalusC's answer is correct - but structure has changed, see just fn:upper-case, fn:lower-case - don't see the ignoreCase option anymore but if you cast it one way or the other to compare, it will work.

function does not take 4 arguments

While building a project , I am seeing an error message like
:ReadFromStream' : function does not take 4 arguments
Can you please suggest me what could be the error.
Below is the line , it is showing the error exits:
hr = var.ReadFromStream(pStm, pMap[i].vt, pMap[i].rgclsidAllowed, pMap[i].cclsidAllowed);
I would appreciate any help .Thanks in advance.
Check the definition of the function, usually this occurs when you are missing a parameter (or have one too many)
If it is supposed to take four of them, make sure one of your parameters isn't the wrong type for the overload you are attempting to use. Sometimes that can make it think you are trying to use a different overload that doesn't use that number.
:ReadFromStream' : function does not
take 4 arguments Can you please
suggest me what could be the error.
That means that ReadFromStream doesn't take 4 arguments.
That means you can't pass 4 objects into it.
(pStm, pMap[i].vt, pMap[i].rgclsidAllowed, pMap[i].cclsidAllowed)
pStm
Map[i].vt
pMap[i].rgclsidAllowed
pMap[i].cclsidAllowed
I think in the definition of ReadFromStream, the method does not have 4 parameters as arguments. Please revisit that.
Related bug on Microsoft Connect

How can I convert a string array to a variant array in VBscript?

I'm using a function in vbscript which returns a variant array of strings.
JobIDs = objDoc.ConnectedSubmit(objServer)
The problem is I can't get the Job ID values from that array, as vbscript doesn't handle typed variables. It just gives a type mismatch when I attempt to do ANYTHING with the JobIDs array. I found some promising information here, but when I used the conversion function:
Set objConverter = CreateObject("ADS.ArrayConvert")
ConvertedJobIDs = objConverter.CStrArray(JobIDs())
It is giving me the same type mismatch error. Am I missing something obvious here? This is, apparently, an official microsoft solution, so I'm not sure why it seems to have the same problem, namely, not being able to actually do anything with a string array in the first place. I've seen the first part of my question answered in many places, all pointing to the MS solution, but I have yet to see any follow up reports of someone successfully using that solution.
I'm not sure if I understand why it doesn't work, so this answer might not be very helpful. I would have thought that something like this might work (following on from your previous question I'm assuming you're trying to get the cancellation to work):
For Each id In JobIDs
WScript.Echo id
YourJob = YourOutgoingFaxQueue.GetJob(id)
YourJob.Cancel()
Next
This behavior is by design, VBScript can't do anything with a non-variant array, there was a KB article from Microsoft that explained this but it is not on-line anymore:
Q165967 - PRB: Script Error Occurs When Referencing Non-variant Array
An archived copy of the article can be found at:
https://ftp.zx.net.nz/pub/archive/ftp.microsoft.com/MISC/KB/en-us/165/967.HTM

Resources