Viewed   120 times

I'm trying to write an iterator for results from a PDO statement but I can't find any way of rewinding to the first row. I would like to avoid the overhead of calling fetchAll and storing all the result data.

// first loop works fine
foreach($statement as $result) {
    // do something with result

// but subsequent loops don't
foreach($statement as $result) {
    // never called 

Is there some way of reseting the statement or seeking the first row?



I'm pretty sure this is database dependent. Because of that, it is something you should try to avoid. However, I think you can achieve what you want by enabling buffered queries. If that doesn't work, you can always pull the result into an array with fetchAll. Both solutions have implications for your applications performance, so think twice about it, if the resultsets are large.

Friday, December 2, 2022

PDO is not good with such things. You need to create a string with placeholders dynamically and insert it into the query, while binding array values the usual way. With positional placeholders it would be like this:

$in  = str_repeat('?,', count($in_array) - 1) . '?';
$sql = "SELECT * FROM my_table WHERE my_value IN ($in)";
$stm = $db->prepare($sql);
$data = $stm->fetchAll();

In case there are other placeholders in the query, you could use the following approach (the code is taken from my PDO tutorial):

You could use array_merge() function to join all the variables into a single array, adding your other variables in the form of arrays, in the order they appear in your query:

$arr = [1,2,3];
$in  = str_repeat('?,', count($arr) - 1) . '?';
$sql = "SELECT * FROM table WHERE foo=? AND column IN ($in) AND bar=? AND baz=?";
$stm = $db->prepare($sql);
$params = array_merge([$foo], $arr, [$bar, $baz]);
$data = $stm->fetchAll();

In case you are using named placeholders, the code would be a little more complex, as you have to create a sequence of the named placeholders, e.g. :id0,:id1,:id2. So the code would be:

// other parameters that are going into query
$params = ["foo" => "foo", "bar" => "bar"];

$ids = [1,2,3];
$in = "";
$i = 0;// we are using an external counter 
// because the actual array keys could be dangerous
foreach ($ids as $item)
    $key = ":id".$i++;
    $in .= "$key,";
    $in_params[$key] = $item; // collecting values into a key-value array
$in = rtrim($in,","); // :id0,:id1,:id2

$sql = "SELECT * FROM table WHERE foo=:foo AND id IN ($in) AND bar=:bar";
$stm = $db->prepare($sql);
$stm->execute(array_merge($params,$in_params)); // just merge two arrays
$data = $stm->fetchAll();

Luckily, for the named placeholders we don't have to follow the strict order, so we can merge our arrays in any order.

Sunday, December 25, 2022

You can use

show databases

or a query on the information_schema:

select schema_name from information_schema.schemata
Monday, September 5, 2022

You need to configure extensions list inside chromeOptions:

capabilities {
    'browserName': 'chrome',
    'chromeOptions': {
        'extensions': ['base64 encoded extension']

Note that it in extensions, it is important to provide a list of base-64 encoded packed Chrome extension.

To get a base64 encoded extension, you need to read the .ctx extension file and encode the contents with base64. For example, using python:

>>> import base64
>>> data = open('path_to_the_ctx_extension').read()
>>> base64.standard_b64encode(data).decode('UTF-8')
# outputs the encoded chrome extension which you can paste in the config

Or, easier, make a helper.js file using fs and q:

var q = require('q');
var fs = require('fs');

exports.getCapabilities = function (filename) {
    var deferred = q.defer();

    fs.readFile(filename, function (err, data) {
        var capabilities = {
            'browserName': 'chrome',
            'chromeOptions': {
                extensions: [

    return deferred.promise;

Then, in your protractor config, use this getCapabilities() function to get capabilities:

var helper = require('./helper.js');

exports.config = {

    capabilities: helper.getCapabilities('/path/to/crx/extension'),


Currently, it works with a single extension, so there is a room for improvement.

Also, look through the following issue in case you have problems:

  • Setting Chrome Options
Monday, August 8, 2022

combine CONCAT() and GROUP_CONCAT():

SELECT MD5(GROUP_CONCAT(CONCAT(col1,col2,...))), 1 as g
  FROM some_table GROUP BY g;
Wednesday, August 31, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :