0

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

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
bzin
  • 1,921
  • 2
  • 14
  • 15
  • 3
    It expects a constant, not a string with a constant name within. – mario Jul 17 '14 at 15:21
  • possible duplicate of [Troubleshooting mysqli\_fetch\_assoc()](http://stackoverflow.com/questions/20555061/troubleshooting-mysqli-fetch-assoc) – Aleksei Matiushkin Jul 17 '14 at 15:33
  • 1
    As Mario says take away the quotation marks from 'PDO:FETCH_ASSOC' – Alberto Gaona Jul 17 '14 at 15:45
  • I tried to do that before posting it here but if I do that I will have a different error saying "Class 'APP\\API\\PDO' not found in ...". This code is inside my own Controller. – bzin Jul 17 '14 at 15:57

1 Answers1

0

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));

}
bzin
  • 1,921
  • 2
  • 14
  • 15