简体   繁体   中英

mysqli query results right number of elements but empty value

i'm working on code above:

<section id="home" style="display:normal;"  data-state="active">
<!-- // tabella con tutte le annate disponibili su dB -->
<?php
    $query = "SELECT year(dataGranpremio) as anno 
              FROM Granpremi
              GROUP BY YEAR (dataGranpremio)";
    $res = mysqli_query($db_connection, $query);
    if (!$res) {
        die ("Errore nella query " . mysqli_error($db_connection));
    }
    $numero = mysqli_num_rows($res);
    <h2>HOME</h2>
    <table width="950" border="1">
    <?php $counter=0;
     while ($counter<$numero) { // ciclo while 
            $countcolumns=0;?>
            <tr>
            <?php while ($ris=mysqli_fetch_array($res) && $countcolumns<$columns) {?>
                <td>
                    <?php  print( $ris);?>
                </td>
                <?php $counter++;
                $countcolumns++;
                }?>
            </tr>
     <?php }?>   <!--// fine ciclo-->
    </table> <!--//chiusura tabella-->
    <?php }// fine if numero
    else { ?><p>ERRORE accesso al DB</p><?php }
    mysqli_close($db_connection);?>
</section>

this section should show the list of all years inside Granpremi table: Granpremi{id(int),dataGranpremio(date)}; i tried the query and it works but when i run this code it shows the correct number of values but empty. Before it worked by mysql i tried to move to mysqli to fix this issue but nothing changed. can someone help me to solve? thank you guys.

I think you are way over-complicating this

You have a loop inside of your loop.

  while ($counter<$numero) { // ciclo while 
        $countcolumns=0;?>
        <tr>
        <?php while ($ris=mysqli_fetch_array($res) && $countcolumns<$columns) {?>
            <td>
                <?php  print( $ris);?>
            </td>
            <?php $counter++;
            $countcolumns++;
            }?>
        </tr>
 <?php }?>

Your inner loop which loops through each record in your sql result set (list of years) is going to execute fully before the outer loop loops once. So you'll end up with a table that has as many records as your result set, and as many fields as there is records in your result set.

Furthermore, you have a condition on your inner where loop (the proper one that loops through records in your result set) that checks that columncounter<columns but you never set $columns so this will fail every time. You won't actually loop through your record set at all. I can't figure out why you have this check at all since you already know your number of columns. It's 1. Your SQL has a single field.

Lastly, I believe mysqli_fetch_assoc() is better option here.

You can turn that into a single row, with multiple fields (horizontal layout):

<h2>HOME</h2>
<table width="950" border="1">    
    <tr>
    <?php while ($ris=mysqli_fetch_aassoc($res)) {?>
        <td>
            <?php  print( $ris["anno"]);?>
        </td>  
    <?php } ?>              
    </tr>     
</table> <!--//chiusura tabella-->

If, instead of a horizontal list for your you wanted a vertical list (multiple rows, and a single column), you could use:

<h2>HOME</h2>
<table width="950" border="1">        
    <?php while ($ris=mysqli_fetch_assoc($res)) {?>
        <tr><td>
            <?php  print( $ris["anno"]);?>
        </td></tr> 
    <?php } ?>              
</table> <!--//chiusura tabella-->

Where we just move the <tr> inside the loop on your record set so a new table row is generated for each record.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM