当前位置: 移动技术网 > IT编程>开发语言>PHP > Symfony2实现从数据库获取数据的方法小结

Symfony2实现从数据库获取数据的方法小结

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网