PG_FETCH_ARRAY()不返回任何数据

时间:2016-02-22 17:46:27

标签: php sql postgresql

我对pg_fetch_array()有以下问题,这是代码:

 //Controllo utente
      $u= "SELECT FROM utente WHERE idutente = '$username' and password= '$password'"; 
                                                     $result = pg_query($db,$u) or die(pg_last_error()); 

                                                     //Controllo query
                                                     if(!$result) {echo "problemi con la query"." ".$u;}

                                                     $tuplaUtente = pg_fetch_array($result);

                                                     //Numero di tuple
                                                     if($tuplaUtente === false) {echo "error fetch";}
                                                     else 
                                                      {   echo $tuplaUtente['idutente'];
                                                          echo "else";
                                                          }

                                                    pg_close();

此代码仅显示" else",尽管使用PgAdmin" $ u"查询返回正确的结果。

事实上如果我输入最后一个:

                                              else 
                                              {   
                                                  var_dump($tuplaUtente);
                                              echo $u;
                                              echo "else";
                                              }
                                                  }

                                            pg_close();

它显示" array(0){} SELECT FROM utente WHERE idutente =' usr'和密码=' pwd'否则"。

1 个答案:

答案 0 :(得分:0)

此查询:

SELECT FROM utente WHERE idutente = '$username' and password= '$password'

没有返回任何列,正如@Jerrad评论的那样。

这在PostgreSQL 9.4之前曾经是无效的SQL,但从那时起被允许,请参阅Release Notes

  

E.7.3.3。查询

     

[...]

     

允许SELECT有一个空的目标列表(Tom Lane)

缺少任何输出列是pg_fetch_array返回的数组为空的原因:结果本身有一行但是列为零。

在查询中显然是一个错误:给定其余的代码,它应该在SELECT之后至少有idutente列。