duanhuancong1969 2011-05-26 04:26
浏览 38
已采纳

php中的postgresql数据库查询问题

I have written this code to display list of items pulled out of a postgresql database, this is a bit lengthy code so I have put little bit of it here to figure out the problem I have. the result I get is given below`test1m

Resource id #4
Galle
Kandy
Anuradhapura
Dambulla
Hambantota
Colombo
Hikkaduwa
test2
Resource id #4

but expected one would be this

test1m
Resource id #4
Galle
Kandy
Anuradhapura
Dambulla
Hambantota
Colombo
Hikkaduwa
test2
Resource id #4 Galle
Kandy
Anuradhapura
Dambulla
Hambantota
Colombo
Hikkaduwa

Here is the code

$date = date("Y-m-d");
$dbh = pg_connect("host=localhost dbname= drupal_test user=usrx password=123");
if (!$dbh) {
  die("Error in connection: " . pg_last_error());
}           

$sql1 = "SELECT * 
           FROM node, modify_dates, node_counter 
          WHERE node.nid = modify_dates.nid 
            AND node.nid = node_counter.nid 
            AND modify_dates.nid = node_counter.nid 
            AND node_counter.totalcount < '1000' 
            AND node.type = 'city_guide' 
       ORDER BY modify_dates.lmd";
$sel1 = pg_query($sql1);

$sql2 = "SELECT * 
           FROM node, modify_dates, node_counter 
          WHERE node.nid = modify_dates.nid 
            AND node.nid = node_counter.nid 
            AND modify_dates.nid = node_counter.nid 
            AND node_counter.totalcount > '1000' 
            AND node.type = 'city_guide' 
       ORDER BY modify_dates.lmd";
$sel2 = pg_query($sql2);

if(pg_num_rows($sel1) > 0) {
  echo "test1";
  chooseItems($sel1);                           
} else if(pg_num_rows($sel2) > 0) {
  echo "test1m<br/>";
  chooseItems($sel2);                           
} 

if(pg_num_rows($sel2) > 0) {
  echo "test2<br/>";
  chooseItems($sel2);                           
} else if(pg_num_rows($sel1) > 0) {
  echo "test2m<br/>";
  chooseItems($sel1);                           
}       

function chooseItems($sel) {
  echo $sel."<br/>";                                    
  $m = 0;

  while($row = pg_fetch_array($sel)) {
    echo $row['title']."<br/>";                 
    $m = $m+1;                  
  }             
}               

pg_close($dbh); 
  • 写回答

1条回答 默认 最新

  • dongxili9934 2011-05-26 05:10
    关注

    The problem is that pg_fetch_array() consumes a row of input. After you read all of the rows, that is it. But the consumption does not affect the number of rows which the query returned. So pg_num_rows() remains constant but when you try to read more data via pg_fetch_array(), there is nothing to read, it has been consumed.

    You could query again to refresh the contents.

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作