Jump to content

quick while question


jesushax

Recommended Posts

the while function loops through records yes?

so if there is nothing to loop through what can i put, it doesnt like else this is what i mean

 

this doesnt work...?

while ($row = mysql_fetch_array($SQL)) {

im loopign records here
}
else {
echo "There are no current records.";
}

Link to comment
https://forums.phpfreaks.com/topic/100102-quick-while-question/
Share on other sites

Method A (most common)

<?php
if (mysql_num_rows($SQL) > 0) {
    while ($row = mysql_fetch_array($SQL)) {

        // im looping records here
    }
} else {
    echo "There are no current records.";
}
?>

 

Method B

<?php
if ($row = mysql_fetch_array($SQL)) {             // returns false if no record
    do {
        // im looping records here
    } while ($row = mysql_fetch_array($SQL));     // use do..while so first row gets processed in the loop
    
} else {
    echo "There are no current records.";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/100102-quick-while-question/#findComment-511850
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.