Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5943723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:26:10+00:00 2026-05-22T16:26:10+00:00

I have written this code to display list of items pulled out of a

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:26:11+00:00Added an answer on May 22, 2026 at 4:26 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of code. This is written for getting the list
I have written this piece of CF code to get and display the data
I have written an android application to display a list. Code has taken from
I have written this code inside a servlet to delete certain records from three
I have written this code in JavaScript and works perfectly fine when I include
I have written this code what it does is if user types postcode or
I have written this code outside all functions: int l, k; for (l =
I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code to convert string in such format 0(532) 222 22
I have written this code in javascript however I have to make it work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.