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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度