1

I'm still very new to PHP and pgsql... new to coding in general. I'm trying to figure out if I should do a while or do while loop on this problem.

I need to query a remote source for data and update my db, but I'm limited to the number of returns per call. I have over 1000 rows to update, but my call limit is 100. This means I need to do multiple calls until all rows in a column are no longer null.

I believe this is the right query, but is my while statement correct?

Here my code:

// $dbconn = connection......
$result = pg_query($dbconn, "WITH data(full_address) AS (VALUES ('location')) 
SELECT full_address FROM $table WHERE latitude is NULL limit 5;");

while ($row = pg_num_rows($result > 0)) {
     $arr = pg_fetch_all($row);

//curl commands fetch data and ingest
}
Shujaat
  • 691
  • 4
  • 18
Dean
  • 211
  • 2
  • 16

2 Answers2

0

Use do while id the loop should get executed atleast once.

While is entry control loop(it will check condition while you are entering the loop)

Do While is exit control loop(it will check condition after executing the loop one time.)

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
0

If you want the loop to run at least once; use do, but if your loop might never be executed (because of the condition) then use while.

In your case, while is prefered since the database query might give no results. Your while loop needs to fetch a single row and process it until there are no more rows to fetch.

while ($row = pg_fetch_row($result)) {
  //your code to use row's data
}

do-while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. The main difference from regular while loops is that the first iteration of a do-while loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it may not necessarily run with a regular while loop (the truth expression is checked at the beginning of each iteration, if it evaluates to FALSE right from the beginning, the loop execution would end immediately).

from: http://php.net/manual/en/control-structures.do.while.php

EDIT

// $dbconn = connection......
for($=0;$i<10;$i++){
$result = pg_query($dbconn, "**your query with** Limit ".(100*$i).",100;");
    while ($row = pg_fetch_row($result)) {
      //your code to use row's data
      // do your curl stuff here for the 1 result
    }
}
Shujaat
  • 691
  • 4
  • 18
  • The loop isn't ending.. it keeps cycling over and over... `$result = pg_query($dbconn, "SELECT full_address FROM $table WHERE latitude is NULL limit 5;"); while ($row = pg_fetch_all($result)) {...` – Dean Nov 19 '15 at 11:54
  • Yes, like this `$result = pg_query($dbconn, "SELECT full_address FROM $table WHERE latitude is NULL limit 5;"); while ($row = pg_fetch_all($result)) { $swears = array("full_address" => "&location", "{" => "", "}" => "", "[" => "", "]" => "", '"' => "", ":" => "="); $json = json_encode($row);` – Dean Nov 19 '15 at 12:00
  • the condition of your `while` loop doesn't match what i am suggesting. you are using a different condition, try to use the code i have suggested – Shujaat Nov 19 '15 at 12:01
  • as I understand it fetch_row pulls one row. $result is an array of rows, so shouldn't it be fetch_all? – Dean Nov 19 '15 at 12:04
  • then why use the loop ? – Shujaat Nov 19 '15 at 12:05
  • the fetch_row gets one row and removes it from the result, so the next time you call it you get the next row, and if you call it a number of times it will start to return false i.e. no more rows left in result. Where as fetch_all returns all results as an array, you can call it a milion times and it will always give you all the results as an array making the loop run forever. and if you already have all the results as an array then you don't need to while loop but rather a for loop to iterate over the results array – Shujaat Nov 19 '15 at 12:09
  • I need to query a remote source for data and update my db, **but I'm limited to the number of returns per call**. I have over 1000 rows to update, but my call limit is 100. This means I need to do multiple calls until all rows in a column are no longer null. My example limits to 5, but operationally, I'll increase it to 99 – Dean Nov 19 '15 at 12:10
  • ahhh... didn't know it removes it.. so I tried this and it still loops indefinitely: `$result = pg_query($dbconn, "SELECT full_address FROM $table WHERE latitude is NULL limit 5;"); while ($row = pg_fetch_row($result)) { $arr = pg_fetch_all($result); $json = json_encode($arr);` – Dean Nov 19 '15 at 12:17
  • for ($i=0;$i<10;$i++){ ?? – Dean Nov 19 '15 at 12:49
  • as you can only get 100 results in one query, you need to run the query 10 times... hence the for loop that will run the query 10 times... – Shujaat Nov 19 '15 at 13:44