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 '<pre class="brush:php;toolbar:false">'; print_r($data);
The result is:
Tom
Example 2:
$conn = $this->getDoctrine()->getConnection(); $data = $conn->fetchArray("SELECT name, color FROM test"); echo '<pre class="brush:php;toolbar:false">'; 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 '<pre class="brush:php;toolbar:false">'; 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 '<pre class="brush:php;toolbar:false">'; 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!