GPiter
GPiter

Reputation: 809

SQLSTATE[HY000]: General error create table in symfony

I have this code :

try{
   $aws = $this->getContainer()->get(Service::class);
   $query = 'DROP TABLE IF EXISTS newtable;CREATE TABLE newtable LIKE actions;';
   $aws->executeQuery($query);
}catch (\Exception $exception){
  $output->writeln("Can't create new tables, with message :");
  $output->writeln(sprintf("%s", $exception->getMessage()));
}

And the Service class function executeQuery :

public function executeQuery($query, $multiple = true, $fetch = true)
{
    $res = $this->conn->prepare($query);
    $result = $res->execute();

    return ($fetch) ? ($multiple ? $res->fetchAll(\PDO::FETCH_ASSOC) : $res->fetch(\PDO::FETCH_ASSOC)) : $result;
}

The table is created but I get the error message : SQLSTATE[HY000]: General error. I don't understand what is the problem that drop this error. Thanks for your help.

Upvotes: 1

Views: 1007

Answers (1)

Robert
Robert

Reputation: 20286

You shouldn't use fetch nor fetchAll on the statements that do not return the data like update/insert/drop/create table

That may be the problem.

Changing

$aws->executeQuery($query);

to

$aws->executeQuery($query, false, false );

should help because therefore only the result of $res->execute() will be returned.

Upvotes: 1

Related Questions