Sunday, December 27, 2015

I Don't Need sqlLoop

What was I thinking?

Yesterday I introduced a new function called sqlLoop().

I can do everything in sqlLoop() with repeated calls to sqlRow().

sqlRow() works as follows. If it is successful, it returns an array full of data. On failure, it returns an array in which the first element is false. My loop looks like:

$row =sqlRow($sql);

while ($row[0]) {
  // process row
  $row = sqlRow(); 
}
The one problem with this code is that if I forget to call sqlRow() in the loop, I've created an infinite loop ... and I am very forgetful.

Hey, I have an idea.

Scrambles back to the drawing board.

If you call sqlRow('chk') then the function returns true if the row exists and false if it doesn't. It maintains a counter to that breaks the loop.
$row =sqlRow($sql);

while (sqlRow(DB_CHK)) {
  // process row
  $row = sqlRow(); 
}
The first form is faster, the second protects from infinite loops. I will put this new code up in a minute.

No comments: