dpwdldgn43486 2013-02-05 18:57
浏览 57
已采纳

php postgresql pg_fetch_all

I'm exeriencing a strange problem with pg_fetch_all (postgresql) : it never returns me more tan 2 columns

For exemple, this code :

    $dbh = pg_connect("host=localhost dbname=dbname user=user password=passwd");
    $query = "SELECT m.id, v.nom, v.id FROM machine m, version v WHERE m.id_version = v.id;";
    $result = pg_query($dbh, $query);
    $array = pg_fetch_all($result);
    print_r($array);

Only prints me m.id, v.nom !

If I paste the exactly same SQL request in pg_my_admin, each colomn is returned.

If I parse my result as

    $array = array();
    while ($row = pg_fetch_row($result)) {
      $array[] = $row;
    }

it works perfectly. Why ?

  • 写回答

1条回答 默认 最新

  • douhan8610 2013-02-05 18:59
    关注

    It is because you have two columns with the same name (in two different tables). You need to use an alias like this :

    $query = "SELECT m.id AS mId, v.nom, v.id AS vId FROM machine m, version v WHERE m.id_version = v.id;";
    

    You will then be able to retrieve the column m.id with the name mId and v.id with the name vId You can find more info about alias in SQL here

    The reason it works with pg_fetch_row() is because this one returns the result based on the index of the column on your query not as an associative array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?