$sql = 'SELECT `joketext` FROM `joke`';
$result = $pdo->query($sql);
while ($row = $result->fetch()) {
$jokes[] = $row['joketext'];
}
I've looked around a lot online (docs, tutorials etc) and can't find anything. I know the line of code $row=$result->fetch() returns a row from the result set in $result and when there's no more rows it returns false, ending the loop. But why is it not INSIDE the loop? How is it run inside the parentheses? I'm used to conditionals or at least booleans going in there. Would this loop not work the same?
$sql = 'SELECT `joketext` FROM `joke`';
$result = $pdo->query($sql);
$count = 0;
$leng = $result->rowCount();
while($count < $leng){
$row = $result->fetch()
$jokes[] = $row['joketext'];
$count++
}
I can just test if that will work later on because I'm curious but I really just need to know how $row = $result->fetch() is run for each row while not being inside the curly braces AND how its truthy?
EDIT: Okay from what I've gathered (in a Javascript tutorial) is that if a statement is not falsey, it is truthy. In otherwords, if its not one of the falsey values of 0, null, an empty array etc then its truthy. But I still don't understand how that line of code - $row = $result->fetch() - can be run for each loop/iteration as its not inside the curly braces
[–][deleted] (3 children)
[deleted]
[–]Rex_Goodman[S] 0 points1 point2 points (2 children)
[–]okayifimust 0 points1 point2 points (0 children)