本文實例講述了Symfony2實現從數據庫獲取數據的方法。分享給大家供大家參考,具體如下:
假設有一張表:test, 字段:name,color;
有2條記錄:
Tom blue
Lily red
示例1:
$conn = $this->getDoctrine()->getConnection();
$data = $conn->fetchcolumn("SELECT name, color FROM test");
echo '<pre>'; print_r($data);
結果為:
Tom
示例2:
$conn = $this->getDoctrine()->getConnection();
$data = $conn->fetchArray("SELECT name, color FROM test");
echo '<pre>'; print_r($data);
結果為:
Array ( [0]=>Tom [1]=>blue )
示例3:
$conn = $this->getDoctrine()->getConnection();
$data = $conn->fetchAssoc("SELECT name, color FROM test");
echo '<pre>'; print_r($data);
結果為:
Array ( [name]=>Tom [color]=>blue )
示例4:
$conn = $this->getDoctrine()->getConnection();
$data = $conn->fetchAll("SELECT name, color FROM test");
echo '<pre>'; print_r($data);
結果為:
Array
(
[0] => Array
(
[name]=>Tom
[color]=>blue
)
[1] => Array
(
[name]=>Lily
[color]=>red
)
)
希望本文所述對大家基於Symfony框架的PHP程序設計有所幫助。