Passing an Array as Arguments in php [duplicate] - php

This question already has answers here:
How can I bind an array of strings with a mysqli prepared statement?
(7 answers)
Closed 1 year ago.
I'm trying to prepare a sql statement with unknown amount of parameters! These parameters are past on in an array. The normal syntax for the function would be:
$stmt->bind_param("string of types",param1,param2,...,paramN)
The problem is I dont know how to add parameters in the function $stmt->bind_param out of an array
I have this code but it does not work:
$stmt= $conn->prepare($request['query']);
if(isset($request['params'])){
call_user_func_array('$stmt->bind_param',$request['params']);
}
$stmt->execute();
$result = $stmt->get_result();
$request['params'] contains the right parameters that need to be added in the function.
But the call_user_func_array gives me this error:
call_user_func_array() expects parameter 1 to be a valid callback, function '$stmt->bind_param' not found or invalid function.
I think call_user_func_array might not be the right function to use!
I googled for hours but could not find a solution for this small problem.

If you're using PHP 5.6+, you could also use the splat operator rather than using call_user_func_array, e.g.
$stmt->bind_param($types, ...$request['params']);
This would be neater for your use-case, since there's an initial argument to bind_param that would otherwise need to be shift-ed onto the front of the array of arguments.

To use a method as a callback parameter to call_user_func_array, use an array with the object name and the method name in it as the callback parameter.
Check PHP's call_user_func_array documentation page for further explanations: http://php.net/manual/pt_BR/function.call-user-func-array.php
// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));

Try something like:
call_user_func_array(array($stmt,'bind_param'),$request['params']);

Related

php pdo bind array parameters in bindParam without foreach loop [duplicate]

This question already has answers here:
Binding multiple values in pdo
(3 answers)
Closed 4 years ago.
Is there a way to bind array params in bindParam function of pdo without using foreach statement? (something like mysql.connector in python).
the foreach version would be like this:
$data = array('name'=>'something','job'=>'something else');
foreach($data as $key => $value){
$stmt->bindParam(':'.$key, $value);
}
Many PDO users think they have to use bindParam(). You don't.
You can pass an array directly to execute() with all your parameter values. It's this easy:
$stmt->execute($data);
If you used named parameters in your SQL, use a hash array. If you used positional parameters, use a plain array.
For more complete code examples, read them here: http://php.net/manual/en/pdo.prepare.php

Modify php default function parameter [duplicate]

This question already has answers here:
Only Variables can be passed by reference error
(2 answers)
Closed 5 years ago.
I have a function yaz_wait() which looks like this
mixed yaz_wait ([ array &$options ] ) and as parameters, it has options as you can see in the linked documentation.
One of the options is timeout value which I want to use and edit from its default 15 seconds to some other value.
I have tried
yaz_wait(array("timeout" => 30));
but I get Fatal error: Only variables can be passed by reference...
I am not sure how exactly should I insert this parameter into this function since I have never met with such parameter type (haven't been working with php a lot).
When you have a function with & parameter in a function this means it will return a reference to the variable instead of the value.
In other words, you need to pass a variable that the function will attempt to change(or do whatever with it). Since you aren't passing a variable, you get a fatal error.
Try changing your code to:
$some_arr = array("timeout" => 30);
yaz_wait($some_arr);

"Cannot pass parameter 2 by reference" Error in PHP [duplicate]

This question already has answers here:
PHP error: "Cannot pass parameter 2 by reference"
(2 answers)
How to resolve 'cannot pass parameter by reference' error in PHP? [duplicate]
(2 answers)
Closed 5 years ago.
When I run this code for updating my likedOne column and making it empty ("")....
$sql11 = $conn->prepare("UPDATE UserData SET likedOne=? WHERE username=?");
$sql11->bind_param('ss',"",$Username);
$Username = "netsgets";
$sql11->execute();
I get this error....
1 Fatal error: Cannot pass parameter 2 by reference in /xxx/xxx/xxx/test.php on line 36.
The line is....
$sql11->bind_param('ss',"",$Username);
What's wrong?
You need to use a variable , bind_param takes only variable not values directly.
$likedone ="";
$sql11->bind_param('ss',$likedone,$Username);
bind_param(...) expects its param parameters as references. See. That means it might change them (e.g. when they refer to a result of the query). Whenever you pass something as a reference you have to give it a name (sloppy rule of thumb, but makes it easier to explain). In a way this just tells PHP that you care about the potential modification (again simplified). You can use $emptyString = ""; $sql11->bind_param('ss',$emptyString ,$Username);
sorry for my previous answers
you could fix it up by changing
$sql11->bind_param('ss',"",$Username);
to
$variable ="";
$sql11->bind_param('ss',$variable,$Username);
because bind_param works better with variables

Do I have to always pass all arguments to PHP function? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP - Calling functions with multiple variables
function test($var1=null, $var2=null, $var3=null){
//smart stuff goes here
}
Do I have to every time call the function passing all variables?
test(null, $var2, null);
I'd like to pass only $var2 because all the other variables have default values... Is it even possible?
In JavaScript we can pass an object to the function, is there something similar in PHP?
You only have to pass the arguments up to and including the last argument you do not wish to use the default value for. In your example, you could do this:
test(null, $var2);
The last argument can be omitted since the default value is satisfactory. But the first one must be included so PHP knows that you are setting the value for the second parameter.
Until PHP offers named parameters like Python does, this is how it has to work.

Unlimited arguments for PHP function? [duplicate]

This question already has answers here:
PHP function with unlimited number of parameters
(8 answers)
Closed 2 years ago.
In php how would you create a function that could take an unlimited number of parameters: myFunc($p1, $p2, $p3, $p4, $p5...);
My next question is: how would you pass them into another function something like
function myFunc($params){
anotherFunc($params);
}
but the anotherFunc would receive them as if it was called using anotherFunc($p1, $p2, $p3, $p4, $p5...)
call_user_func_array('anotherFunc', func_get_args());
func_get_args returns an array containing all arguments passed to the function it was called from, and call_user_func_array calls a given function, passing it an array of arguments.
Previously you should have used func_get_args(), but in a new php 5.6 (currently in beta), you can use ... operator instead of using .
So for example you can write :
function myFunc(...$el){
var_dump($el);
}
and $el will have all the elements you will pass.
Is there a reason why you couldn't use 1 function argument and pass all the info through as an array?
Check out the documentation for variable-length argument lists for PHP.
The second part of your question refers to variable functions:
...if a variable name has parentheses appended to it, PHP will look for a function with the
same name as whatever the variable evaluates to, and will attempt to execute it.

Categories