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); 
6
  • What does the sel2 SQL query return when you run it in psql? We don't have enough information to know whether your claims make sense or not, since they depend on the data in the database. Commented May 26, 2011 at 4:33
  • Are you using Drupal itself, or just trying to extract data from the Drupal schema? I ask because Drupal has convenience methods for handling the database dirty work for you. Commented May 26, 2011 at 4:37
  • @Seth Robertson: I understand it totally depends. query returns these data Galle Kandy Anuradhapura Dambulla Hambantota Colombo Hikkaduwa Commented May 26, 2011 at 4:42
  • No, I'd like to see the exact output of psql pasted, including the column headers and numbers of rows returned. Commented May 26, 2011 at 4:44
  • @KyleWpppd: I'm just trying to extract data ... Commented May 26, 2011 at 4:44

1 Answer 1

2

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.

2
  • Thanks a lot for your explanation, now it makes lot of sense :) Commented May 26, 2011 at 5:33
  • You don't have to query again, you can reset the counter. There's a command for that but I can't remember it right off the top of my head. Commented May 26, 2011 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.