1

I have this code and want you ask if the while ($row = mysqli_fetch_assoc($result_table_main)) ends when there are no rows anymore in result set? Do not understand why ist ends because the while has no counter. This is the only question in my post. Maybe you can give me a proper explaination.

I worked very long on it, because I did not know very much about this before. Now I need to doucument my work.

Thanks for your answer!

<?php
$sql_table_main = "SELECT id,name_Gleitzeitrahmen, name_Abweichungen, name_Mehrarbeitervolumen, name_Mehrarbeit1, name_Ausgleich, name_Mehrarbeit2, name_Personalmassnahmen, name_Ueberstunden, name_Ueberstunden_abzusehen, name_Klaerungsbedarfe1, name_Klaerungsbedarfe2 FROM landrat_dashboard";
$result_table_main = mysqli_query($con, $sql_table_main);
?>

<div class="card-content table-responsive table-maxheight" style="overflow:scroll;">
<table class="table table-hover table-mc-green table-bordered table-striped table-condensed">
    <thead class="text-primary">
        <th class="thbackground_green">ID</th>
        <th class="thbackground_green">Gleitzeitrahmen</th>
        <th class="thwidth thbackground_green">Abweichungen</th>
        <th class="thwidth thbackground_green">Mehrarbeitervolumen</th>
        <th class="thwidth thbackground_green">Mehrarbeit</th>
        <th class="thwidth thbackground_green">Ausgleich</th>
        <th class="thwidth thbackground_green">Mehrarbeit</th>
        <th class="thwidth thbackground_green">Personalma&szlig;nahmen</th>
        <th class="thwidth thbackground_green">&Uuml;berstunden im Rahmen?</th>
        <th class="thwidth thbackground_green">Sind &Uuml;berstunden abzusehen?</th>
        <th class="thbackground_green">Kl&auml;rungsbedarfe</th>
        <th class="thwidth thbackground_green">Kl&auml;rungsbedarfe Beschreibung</th>
    </thead>
    <tbody>
        <?php
        if(mysqli_num_rows($result_table_main) > 0){
            while ($row = mysqli_fetch_assoc($result_table_main)) {
            echo '<tr>';
            echo '<td>'. $row['id'] .'</td>';
            echo '<td>'. $row['name_Gleitzeitrahmen'] .'</td>';
            echo '<td>'. $row['name_Abweichungen'] .'</td>';
            echo '<td>'. $row['name_Mehrarbeitervolumen'] .'</td>';
            echo '<td>'. $row['name_Mehrarbeit1'] .'</td>';
            echo '<td>'. $row['name_Ausgleich'] .'</td>';
            echo '<td>'. $row['name_Mehrarbeit2'] .'</td>';
            echo '<td>'. $row['name_Personalmassnahmen'] .'</td>';
            echo '<td>'. $row['name_Ueberstunden'] .'</td>';
            echo '<td>'. $row['name_Ueberstunden_abzusehen'] .'</td>';
            echo '<td>'. $row['name_Klaerungsbedarfe1'] .'</td>';
            echo '<td>'. $row['name_Klaerungsbedarfe2'] .'</td>';
            echo '</tr>';
            }
        }
        ?> 
    </tbody>
</table>

Ck2513
  • 107
  • 9

2 Answers2

1
       <?php
    if(mysqli_num_rows($result_table_main) > 0){
        while ($row = mysqli_fetch_assoc($result_table_main)) {
        echo '<tr>';
        echo '<td>'. $row['id'] .'</td>';
        echo '<td>'. $row['name_Gleitzeitrahmen'] .'</td>';
        echo '<td>'. $row['name_Abweichungen'] .'</td>';
        echo '<td>'. $row['name_Mehrarbeitervolumen'] .'</td>';
        echo '<td>'. $row['name_Mehrarbeit1'] .'</td>';
        echo '<td>'. $row['name_Ausgleich'] .'</td>';
        echo '<td>'. $row['name_Mehrarbeit2'] .'</td>';
        echo '<td>'. $row['name_Personalmassnahmen'] .'</td>';
        echo '<td>'. $row['name_Ueberstunden'] .'</td>';
        echo '<td>'. $row['name_Ueberstunden_abzusehen'] .'</td>';
        echo '<td>'. $row['name_Klaerungsbedarfe1'] .'</td>';
        echo '<td>'. $row['name_Klaerungsbedarfe2'] .'</td>';
        echo '</tr>';
        }
    }
    ?> 

This means: It the number of rows in the rusult is greater than 0, then do for every row in the results the echos.

So yes:

if the while ($row = mysqli_fetch_assoc($result_table_main)) ends when there are no rows anymore in result set?

Alpix
  • 98
  • 1
  • 9
  • Ok thanks but why do i nedd a while here? what is the use? – Ck2513 Jan 16 '18 at 12:15
  • while there are rows, do ... If there are no mor rows it returns null interpreted same way as false, so while(false) ends the while construct. Btw, did you know that you can approve right answers? – Alpix Jan 16 '18 at 12:16
  • If those are all your columns in the table you don't need to add them 1 by 1, just use echo ''.$row.", while loop will check everyone column, thats why it is used here. – Nick Jan 16 '18 at 12:19
1

A quote from the documentation of the function mysqli_fetch_assoc:

Return Value: Returns an associative array of strings representing the fetched row. NULL if there are no more rows in result-set

So if there are no more rows to fetch the function will return null. If you look at the while loop condition it will look like this (only when there are no more rows):

$row = null

Which will evaluate to false, so the condition is not met and therefore the loop will end.

kscherrer
  • 5,486
  • 2
  • 19
  • 59
  • Ok i understand now. Thank you! So mysqli_fetch_assoc has an counter which counts the entrys in the $result_table_main and reduce it every pass? – Ck2513 Jan 16 '18 at 12:37
  • I'm not sure on how it is handled exactly, but my guess would be this: the result is an array and everytime you fetch a row from it with mysqli_fetch_assoc it takes the first array-item (which happens to be an array too but nvm) and removes that item from the result array, kinda like [array_shift](http://php.net/manual/en/function.array-shift.php). Again: I'm not certain on this but this is how I understand it. – kscherrer Jan 16 '18 at 12:54
  • Ok thank you! This helped me, but i can not find anything in the documentation about this possible counter, so i think your idea could be right. – Ck2513 Jan 16 '18 at 13:06
  • I just re-read your code and I wanted to mention that you dont need the `if(mysqli_num_rows($result_table_main) > 0){` around the while loop. If you used a do-while loop it would be needed, but a while loop does not execute even once if initially the condition is not met. so even if the `$result_table_main` had no rows at start, nothing would go wrong. – kscherrer Jan 16 '18 at 13:24
  • Ok, that is interesting so without the if i have to use do-while. – Ck2513 Jan 16 '18 at 13:58
  • no, keep the while. You would need the if, if you used do-while - **edit:** [read more about do-while vs while](https://stackoverflow.com/questions/3347001/do-while-vs-while) – kscherrer Jan 16 '18 at 13:59