I am trying to use FETCH_LAZY. I can use a while loop whilst setting $row equal to $query->fetch(). The problem is that fetchAll does not work when the fetchMode is FETCH_LAZY.
How can I return an array of PDO Objects?
Thanks.
Due to the very nature of FETCH_LAZY, you cannot use fetchAll with this mode. It can be used with fetch() only.
It seems that you are simply confusing this mode with FETCH_OBJ which will give you an array of objects you need
$array = $stmt->fethAll(PDO::FETCH_OBJ);
Also note that beside creating stdObj instances you can make fetchAll to return an array of objects of any other class as well.
Related
I need to get data in PHP from PDO with some transformations of data during the fly. The best variants are FETCH_CLASS mode and FETCH_FUNC mode.
but the problem of FETCH_CLASS is that each property need to go throw __set() method to get transformation or at least to check if it needs transformation. And for 40K rows with 50 columns each it takes too much time.
FETCH_FUNC seems ideal for me cause i get all the columns of the row at once, can make some transforms BUT, the only problem is that i don't have column names cause column values come into function as parameters and the only way to get them is to call func_get_args().
So generally the question is -> How can i get row data at once in some method of the class, function, using some combination of PDO FETCH modes as an assoc array, obj or some like columnName->columnValue structure? Using foreach after getting result is to long - i want to get transformed data already after calling pdo's fetchAll() method
Seems to me like you're overthinking this and are trying to be too clever. This will do just fine:
$data = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$data[] = myTransformation($row);
}
In other words, just fetch with FETCH_ASSOC, then call your function on that result.
I'm using json_encode to dump an array and I'm getting different results using array() and SplFixedArray. It gives me array as it should when I use array(), but gives me an object when I use an SplFixedArray. Is there a reason for this?
This is what is returned when I use a normal array():
[{"firstName":"Bo","middleName":"N","lastName":"Higgs"},
{"firstName":"Bob","middleName":"D","lastName":"Superstar"}]
And when I use a SplFixedArray it gives me:
{"0":{"firstName":"Bo","middleName":"N","lastName":"Higgs"},
"1":{"firstName":"Bob","middleName":"D","lastName":"Superstar"}}
The two string are exactly the same, SplFixedArray only has a little different way of storing it, as it is a FIXED array. simple demo
Also be sure to read the manual. If you initiate SplFixedArray (it is a class, not a function) you get an object in return. The manual:
http://php.net/manual/en/class.splfixedarray.php
Ive been running some queries using PHP's PDO library. It seems that when i use:
<?php
$smtp->execute();
$result = stmt->fecthArray();
?>
It unsets the array inside PDO. I know this because when i call that very same line again, it returns an empty array. Why does it do this? Is this normal behavior?
When building the resulting array, fetchAll() removes all the results from the result set. Instead of calling it again, re-use the array you retrieved in the first time.
My MySQL queries are returning arrays with duplicate entries: numbered keys and labeled keys with the same data inside. This may be standard, but it seems like a waste, and something that could cause problems if I'm printing values. I mean, not a huge problem, obviously. But I'm just curious if I can stop that. It seems unnecessary. For example:
Array(
[0] => "Ted",
[first_name] => "Ted",
[1] => "Schmidlap",
[last_name] => "Schmidlap"
)
And so on.
I'm pretty new to a lot of this, so this may be a simple question, but Googling doesn't seem to have any answers for me. Anyone know the reason this happens? I'm using PHP's PDO now, but I was doing it straight through the MySQL functions before and the same thing was happening, so I assume it's a byproduct of MySQL interaction.
I can iterate through and unset the numeric ones, because I don't need them, but they're not really in the way right now, so that's just an extra step. Still, is there a way to simply not have them fetched in the first place?
Presumably this is happening after you use mysql_fetch_array (or similar).
You need to add in a flag to specify what array type you want returned or PHP assumes both.
i.e. mysql_fetch_array($result_set, MYSQL_ASSOC|MYSQL_NUM|MYSQL_BOTH)
That depends on the function you are using.
Some functions return both types, others return only one of them.
If you are using PDOStatement->fetch, notice the optional $fetch_style argument it takes.
Your issue is with the mysql_fetch_array function.
If you want only the numbers, use:
$row = mysql_fetch_array($result, MYSQL_NUM)
If you want only the text indexes, use:
$row = mysql_fetch_array($result, MYSQL_ASSOC)
I usually use MySQLi but for PDO you can find more information here: http://us3.php.net/manual/en/pdostatement.fetch.php
Which seems to mean that you should be using this for text indexes:
$row = $statement->fetch(PDO::FETCH_ASSOC);
And this for numeric indexes:
$row = $statement->fetch(PDO::FETCH_NUM);
Also, just to note this since it isn't listed here, if you want an associative array, you can use mysql_fetch_assoc(), or if you want an enumerated (numbered) array use mysql_fetch_row() instead of mysql_fetch_array(). I use this mostly because without it I would often forget the flag, so I got into the habit of just using the specific function.
I am confused about this, I run a query such as
foreach($dbh->query("SELECT * FROM ...") as $row) {
...do something with row()
But when I var_dump $dbh it is a PDOstatement object.
This leads me to two questions:
How does foreach somehow resolve the object into separate rows?
How can I store all rows in an array, like $arr = $dbh->query(...)? This does not work because it is still an object
I can of course run under the foreach, and do $arr[] = $row, but that seems a bit silly..
PHP has some interesting interfaces that let you handle objects as arrays. In this case the PDOStatement class implements Traversable and this allows the use of foreach on it. See here http://www.php.net/manual/en/class.pdostatement.php
To get all your results in one array take a look at PDOStatement::fetchAll. And also in the future consider checking the PHP documentation on php.net if you run into problems. It's packed with examples and useful user comments.
Use PDOStatement::fetchAll() to get the entire result set as array. AS far as what PDO does internally it uses an iterator to access some representation of a recordset. This way you use less memory because youre not pulling the entire result set into php, youre using something resembling a resource thats internal to the implementation.