Search code examples
phpmysqlsymfonysilexdbal

When I try to change the default fetch style with Silex framework I get "MysqliException: Unknown fetch type 'FETCH_ASSOC'"


I am doing some tests with Silex to develop a REST API but I encounter an issue when I am fetching data from database.

MysqliException: Unknown fetch type 'FETCH_ASSOC'"

When I do not add give any fetch type I receive the data correctly with PDO::FETCH_BOTH which can be annoying when table are with a lot of fields and they repeat themselves by number and column name.

public function getData(Application $app, $parentId){

  $sql = "SELECT * FROM table WHERE parent_id = '$parentId' AND active = '1' ORDER BY label";
  $rs = $app['db']->query($sql);

  return $app->json($rs->fetchAll('PDO::FETCH_ASSOC'));

}

I register my database has MySqli:

$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
    'db.options' => array(
        'driver' => 'mysqli',
        'host' => 'localhost',
        'dbname' => 'database_name',
        'user' => 'user',
        'password' => 'password',
    ),
));

I would actually understand what I am doing wrong.

Do anyone knows why this happens and how to solve it?

Some links about the matter in question: http://www.doctrine-project.org/api/dbal/2.2/class-Doctrine.DBAL.Driver.Mysqli.MysqliStatement.html


Solution

  • Ok, I just add it has a constant with a \ before the style and it worked.

    public function getData(Application $app, $parentId){
    
      $sql = "SELECT * FROM table WHERE parent_id = '$parentId' AND active = '1' ORDER BY label";
      $rs = $app['db']->query($sql);
    
      return $app->json($rs->fetchAll(\PDO::FETCH_ASSOC));
    
    }