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'];
Related
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
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:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I'm a complete beginner in learning PDO for PHP and I actually haven't learned MySQL or MySQLi yet.
(Please take a look at the code below) I'm trying to make sense of what this " -> " arrow means and I couldn't find an answer anywhere else. Is the arrow semantically equivalent to the action word "perform" in every day english?
E.G for the codes below, $stmt (perform) -> closeCursor();
Code:
$stmt = $db->prepare($sql);
$stmt->execute(array($title,$entry));
$stmt->closeCursor();
Thank you.
The arrow is part of PHP's object syntax, it's saying:
$object->method();
In English is:
Run method on object
It's also used for accessing properties.
As PDO is a class in PHP and variables like $db are instances of that class, you're able to make use of the methods and properties in those instances.
Check out PHP's object docs for more info on the subject, and if you're new to Object Oriented programming then you'll need to research the subject.
That is php's object notation equivilent to . in Java and Javascript. Basically it's used to access a method or property of an object.
See the Objects and Classes documentation.
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:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(2 answers)
Closed 2 years ago.
I am new to mysqli and started trying to learn basic things. With respect to this i example (http://php.net/manual/en/mysqli-result.fetch-array.php) i was trying fetch_array. Here is my code.
$sqlGetChartData = "SELECT date, ratepersqft, location
FROM ratepersqft
WHERE project_id = 1";
$runGetChartData = $mysqli->query($sqlGetChartData);
while($rowGetChartData = $runGetChartData->fetch_array(MYSQLI_BOTH))
$arrGetChartData[] = $rowGetChartData;
print "<pre>";
print_r($arrGetChartData);
exit();
Here i am getting this error Call to a member function fetch_array() on a non-object on line next to while condition line. I tried googling it and did not get result for my problem. Hope my question is clear. Thanks in Advance.
This answer has been written very long time ago and become irrelevant.
Since then I learned the proper solution for this problem and wrote it in this answer. Please navigate there.
The query probably failed and mysqli::query returned FALSE. Therefore $runGetChartData is not a mysqli_result object, but a boolean, which is why you are getting your error.
From the documentation:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.