# Use of the return value of StatementInterface::execute()

**URL:** <https://discourse.laminas.dev/t/use-of-the-return-value-of-statementinterface-execute/1527>\
**Category:** Components & MVC\
**Tags:** laminas-db\
**Created:** [April 14, 2020, 3:13pm UTC](https://discourse.laminas.dev/t/use-of-the-return-value-of-statementinterface-execute/1527 "2020-04-14T15:13:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CDW](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/cdw/32/671_2.png) [@CDW](https://discourse.laminas.dev/u/CDW)\
**Post date:** [April 14, 2020, 3:13pm UTC](https://discourse.laminas.dev/t/use-of-the-return-value-of-statementinterface-execute/1527/1 "2020-04-14T15:13:18Z")

</div>

Hi i am having some trouble in a part to retrieve data from my database in order set a multi-language application, where i would on app start to redirect to the default language that i have predefined as a column as of type Bool/TinyINT, but as I connect and try to execute a statement to retrieve all the languages list in my database table it won’t return the results. Can someone help me here? Here is my class.

```
namespace Nanocord\Mvc;

// Load classes
use Laminas\Mvc\Application as LaminasApplication;
use Laminas\Db\Adapter\Adapter;
use Laminas\Db\Sql\Sql;

/**
 * Application
 * 
 * @category Nanocord
 * @package Mvc
 * @subpackage Application
 */
class Application extends LaminasApplication 
{
    private $config;
    private $db;
    private $isConnected;
    private $sql;
    
    public function __construct($config) 
    {
        $this->config = $config;
    }

    public function start() 
    {
        $application = parent::init($this->config)->run();
        
        $db_config = $application->getConfig()['db'];
        $db_adapter = new Adapter($db_config);
        $db_adapter->driver->getConnection()->connect();
        
        if ($db_adapter->driver->getConnection()->isConnected() === true) {
            
            $this->db = $db_adapter;
            $this->isConnected = true;
            $this->sql = new Sql($this->db);
            $this->initLanguage();
            
        }
    }
    
    public function initLanguage() 
    {
        $select = $this->sql->select();
        $select->from('application_language');
        
        $statement = $this->sql->prepareStatementForSqlObject($select);
        $results = $statement->execute();
        
        var_dump($results);
    }
      
    public function initSession() 
    {

    }
}
```

---

<div class="post-metadata">

**Author:** ![visto9259](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/visto9259/32/669_2.png) [@visto9259](https://discourse.laminas.dev/u/visto9259)\
**Post date:** [April 14, 2020, 6:28pm UTC](https://discourse.laminas.dev/t/use-of-the-return-value-of-statementinterface-execute/1527/2 "2020-04-14T18:28:39Z")

</div>

Are you expecting `$results` to contain the results from the query? If so, you are missing a few steps.

`$statement->execute()` returns a `ResultSet` and not an array of data rows.

`ResultSet` allows you to iterate through the rows returned by your query.

```php
// To get the current row
$results->current()

// Then go to the next row
$results->next();

// ResultSet are Iterators, use this to iterate through each row
foreach($results as $result) {
   // do something with $results as it contains the data from the row
}

```

Check out the `ResultSet` documentation in the `laminas-db` component documentation.

I hope that I got your question right and this answers it.

---

<div class="post-metadata">

**Author:** ![CDW](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/cdw/32/671_2.png) [@CDW](https://discourse.laminas.dev/u/CDW)\
**Post date:** [April 14, 2020, 6:46pm UTC](https://discourse.laminas.dev/t/use-of-the-return-value-of-statementinterface-execute/1527/3 "2020-04-14T18:46:36Z")

</div>

Yes looping the results works but I thought by dumping the statement execuet would also give me an idea of what it would give out as an output

---

<div class="post-metadata">

**Author:** ![visto9259](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/visto9259/32/669_2.png) [@visto9259](https://discourse.laminas.dev/u/visto9259)\
**Post date:** [April 14, 2020, 6:56pm UTC](https://discourse.laminas.dev/t/use-of-the-return-value-of-statementinterface-execute/1527/4 "2020-04-14T18:56:29Z")

</div>

Not quite.

I never looked under the hood but I would think that this is an elaboratd abstraction of PHP’s `PDOStatement::execute` which does not return data until you call `PDOStatement::fetch`.

Somewhere within `ResultSet`, when you call the `count()` or `current()` or iterate through ir, the `PDOStatement::fetch` gets called.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [April 15, 2020, 7:11am UTC](https://discourse.laminas.dev/t/use-of-the-return-value-of-statementinterface-execute/1527/5 "2020-04-15T07:11:39Z")

</div>

> [@CDW](#):
>
> Here is my class.

The MVC component is event driven and therefore you can use a listener which is listen to one of [the existing MVC events](https://docs.laminas.dev/laminas-mvc/mvc-event/#order-of-events). In the listener you can execute your code.  
This allows to define when your code will be executed (listener is triggered) and you can access the servicemanager of your application (to get the database adapter or something else).

Some examples can be found in the documentation or here in the forum.

One of the examples: [Redirecting...](https://docs.laminas.dev/laminas-view/cookbook/setting-module-specific-layouts/)

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [April 15, 2020, 11:33am UTC](https://discourse.laminas.dev/t/use-of-the-return-value-of-statementinterface-execute/1527/7 "2020-04-15T11:33:41Z")

</div>

You marked this thread as solved therefore please open new thread if you have a new question. Otherwise the overview is lost.  
Thanks in advance!
