Symfony2: a summary of how to obtain data from the database. symfony2 _ PHP Tutorial

Source: Internet
Author: User
Symfony2 summarizes the methods used to obtain data from the database, and symfony2. Symfony2: a summary of how to obtain data from the database. symfony2 this article describes how to obtain data from the database in Symfony2. Share it with you for your reference. for details, refer to Symfony2's method summary for getting data from the database and symfony2's summary.

This example describes how to obtain data from a database using Symfony2. We will share this with you for your reference. The details are as follows:

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

Example 1:

$ Conn = $ this-> getDoctrine ()-> getConnection (); $ data = $ conn-> fetchcolumn ("SELECT name, color FROM test"); echo'
'; print_r($data);

Result:

Tom

Example 2:

$ Conn = $ this-> getDoctrine ()-> getConnection (); $ data = $ conn-> fetchArray ("SELECT name, color FROM test"); echo'
'; print_r($data);

Result:

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

Example 3:

$ Conn = $ this-> getDoctrine ()-> getConnection (); $ data = $ conn-> fetchAssoc ("SELECT name, color FROM test"); echo'
'; print_r($data);

Result:

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

Example 4:

$ Conn = $ this-> getDoctrine ()-> getConnection (); $ data = $ conn-> fetchAll ("SELECT name, color FROM test"); echo'
'; print_r($data);

Result:

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

I hope this article will help you design PHP programs based on the Symfony framework.

Articles you may be interested in:
  • Symfony2 joint query implementation method
  • Symfony2 create page instance details
  • Analysis on date usage in twig of symfony2.4
  • Session and cookie usage in Symfony2
  • Symfony2 framework learning Notes
  • Symfony2 study notes plugin format analysis
  • System route details for Symfony2 study notes
  • A detailed explanation of the controller usage of Symfony2 learning Notes
  • Template usage of Symfony2 learning Notes
  • How to install a third-party Bundles instance in Symfony2
  • Symfony2 function usage example analysis

Examples in this article describes how to obtain data from a database using Symfony2. Share it with you for your reference, as shown in...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.