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
Related
This question already has answers here:
How to get single column values using MySQLi?
(4 answers)
Closed 2 years ago.
I fetch a column value during a query, I store each row value in an array:
while($stmt->fetch()){
$category_array[]=array($category);
}
The result looks like this in the console:
category_array: Array(3)
0: ["Automotive Care"]
1: ["Automotive Care"]
2: ["Digital Service"]
I am trying to combine these into a single array that looks like:
["Automotive Care","Automotive Care","Digital Service"]
This array may have more than these results. So it has to be able to process whatever comes its way.
I am unable to find the right PHP function that allows me to do this. Which function should I use?
thanks!
Simply change the code to remove the unnecessary array()
while($stmt->fetch()){
$category_array[] = $category;
}
This question already has answers here:
PDO::FETCH_OBJ php
(2 answers)
Closed 5 years ago.
hey guys please tell me what does this argument mean in the PDO fetchAll function
fetchAll(PDO::FETCH_OBJ);
I've looked it up in the manual but I've found nothing about it, I've got it from a tutorial but I dunno what it means
All PDO "fetch" methods, requests an optional parameter called $fetch_style that means the data structure which your entity will be returned, when you use PDO::FETCH_OBJ it means that your entity will be an stdClass instance, if you use PDO::FETCH_ASSOC it will return an associative array with the entity data, for more information, check the link below:
http://php.net/manual/en/pdostatement.fetch.php
Edit: The method PDO::fetchAll() will return an array with the entities found by your statement, so if you use the PDO::FETCH_OBJ you will access your entites like:
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
$result[0]->field;
If you use the PDO::FETCH_ASSOC fetch style, you will access your entity using:
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$result[0]['property'];
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']);
This question already has answers here:
How to create a JSON object
(5 answers)
Closed 2 years ago.
Im using PHP and I need a way to convert an entire recordset to a JSON string.
While searching Stack Overflow I found this solution that works:
function recordSetToJson($mysql_result) {
$rs = array();
while($rs[] = mysql_fetch_assoc($mysql_result)) {
// you donĀ“t really need to do anything here.
}
return json_encode($rs);
}
The problem with this code is that I found that the function mysql_fetch_assoc() is deprecated in PHP 5.5.0. Another thing is that im using PDO to connect to my database.
Given the circunstances above, what would be the best solution to convert a PDO recordset to JSON? I want it to work at later versions of PHP too.
The solution is simple.
Considering that the variable $stmt is your PDO recordset, you can convert it to JSON like this:
json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
For more info about the functions used in this piece of code:
http://www.php.net/manual/en/function.json-encode.php
http://www.php.net/manual/en/pdostatement.fetchall.php
You should use something like that
somewhere previously
$stmt = $pdo->prepare("SELECT * FROM fruit WHERE name = ?");
$stmt->execute(array("Apple"));
....
function recordSetToJson($stmt) {
$json_result = array();
while($tmp = $stmt->fetch() ) {
$json_result[] = $tmp;
}
return json_encode($json_result);
}
But final solution will be totally depends form too many factors.
This question already has answers here:
Call dynamic Function with unknown number of params
(3 answers)
Closed 8 years ago.
I want to do something like that:
call_user_func(array('Class', 'Method'), implode(", ", $array));
Is it possible? The imploded string contains a couple of expressions. In this shape however, if $array contains 2 elements, I get an error that Class::method() needs 2 parameters, not 1.
How can I solve this?
You want call_user_func_array instead.
call_user_func_array(array('Class', 'Method'), $array);