symfony2 数据库原生查询
1. 数组,没有键名 但只查询出第一个结果
$conn = $this->getDoctrine()->getConnection(); $data = $conn->fetchArray('SELECT * FROM user where id < 3');
// 或者
// $data = $conn->fetchArray('SELECT * FROM user where id < ?', [3]);
print($data);die;
上面结果是数组
Array ( [0] => 1 [1] => lili )
2.数组,有键名 但是只查询出第一个结果
$conn = $this->getDoctrine()->getConnection(); $data = $conn->fetchAssoc('SELECT * FROM user where id < 3');
// 或者
// $data = $conn->fetchAssoc('SELECT * FROM user where id < ?', [3]);
print($data);die;
结果:
Array ( [id] => 1 [nickname] => lili )
3.多个值
$conn = $this->getDoctrine()->getConnection(); $data = $conn->fetchAll('SELECT id,nickname FROM user where id < 3'); print_r($data);die;
结果:
Array ( [0] => Array ( [id] => 1 [nickname] => lili ),
[1] => Array
(
[id] => 1
[nickname] => lala
)
)
4.一般不用 显示第一个结果,还是结果的第一个字段
$conn = $this->getDoctrine()->getConnection(); $data = $conn->fetchcolumn('SELECT id,nickname FROM user where id < 3'); print_r($data);die;
结果
1