search
Homephp教程PHP开发Summary of how Symfony2 implements obtaining data from the database

The example in this article describes the method of obtaining data from the database in Symfony2. Share it with everyone for your reference, the details are as follows:

Suppose there is a table: test, fields: name, color;
There are 2 records:
Tom blue
Lily red

Example 1:

$conn = $this->getDoctrine()->getConnection();
$data = $conn->fetchcolumn("SELECT name, color FROM test");
echo &#39;<pre class="brush:php;toolbar:false">&#39;; print_r($data);

The result is:

Tom

Example 2:

   
$conn = $this->getDoctrine()->getConnection();
$data = $conn->fetchArray("SELECT name, color FROM test");
echo &#39;<pre class="brush:php;toolbar:false">&#39;; print_r($data);

The result is:

Array
(
  [0]=>Tom
  [1]=>blue
)

Example 3:

$conn = $this->getDoctrine()->getConnection();
$data = $conn->fetchAssoc("SELECT name, color FROM test");
echo &#39;<pre class="brush:php;toolbar:false">&#39;; print_r($data);

The result is:

Array
(
  [name]=>Tom
  [color]=>blue
)

Example 4:

$conn = $this->getDoctrine()->getConnection();
$data = $conn->fetchAll("SELECT name, color FROM test");
echo &#39;<pre class="brush:php;toolbar:false">&#39;; print_r($data);

The result is:

Array
(
  [0] => Array
    (
      [name]=>Tom
      [color]=>blue
    )
  [1] => Array
    (
      [name]=>Lily
      [color]=>red
    )
)

I hope this article will be helpful to everyone’s PHP program design based on the Symfony framework.

For more relevant articles on how Symfony2 implements methods of obtaining data from the database, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools