How you can use the sql queries in Symfony2

In this tutorial I will show a simple way to use the sql queries in Symfony2. As you might know, in Symfony you can use the Doctrine to extract snd insert data in your database. Also, you can use the sql queries to extract, update and insert data in your database.

If you don't wanna use the Doctrine, you can use the sql queries.

To use the sql queries you must first add this code:

$connection = $this->get('database_connection');

in your action.

Then you can use this code:

$connection->fetchColumn("SELECT `text` FROM `table` WHERE `id`='9'");

to extract data from a column. If you want extract data from all columns in a table, you must use this code:

$connection->fetchAll("SELECT `text` FROM `table`");

If you want update data in your table, you can use this code:

$connection->executeUpdate("UPDATE `table` SET `text`='new information' WHERE id='1'");

And if you want delete data from your table, use this code:

$connection->executeUpdate('DELETE FROM `table`);

Video tutorial:









Leave a Comment