简体   繁体   中英

PHP: Show data from mysql database in table

I am new to php and trying to fetch data from database and trying to show it in html table. My issue is total number of returned record is 13 but in table it is just showing 12 record( it is skipping first record in html table) my code snippt is as below

$num_rows = mysql_num_rows($result);
echo $num_rows;
if ($num_rows > 0) {
    $i = 0;
    $dyn_table = '<table class="gridData">';
    $dyn_table.= '<th>Mil Purchy</th><th>Next Mil Purchy</th><th>total Missing</th><th>Missing From-To</th>';

    for ($x = 0; $x <= $num_rows; $x++) {
        while ($row = mysql_fetch_array($result)) {
            if ($i % 1 == 0) {
                 // if $i is divisible by our target number (in this case "3")
                echo "<script> alert('$i') </script>";
                $dyn_table.= '<tr><td>' . $row[0] . '</td>';
                $dyn_table.= '<td>' . $row[1] . '</td>';
                $dyn_table.= '<td>' . $row[2] . '</td>';
                $dyn_table.= '<td>' . $row[3] . '</td>';
            } else {
                echo "<script> alert ('in else statement') </script>";
            }
            $i++;
        }
        $dyn_table.= '</tr></table>';
    }
    echo $dyn_table;
}

and on submit button I have written following code

<fieldset>
    <?php
    if ($_GET) {
        if (ISSET($_GET['submit'])) {

            // echo "<script> alert('waiting for function') </script>";
            $From_Date = $_GET['DateFrom'];
            $To_Date = $_GET['DateTo'];
            Sequece($From_Date, $To_Date);
        }
    }
    ?>
</fieldset>

before anything it is important that you use mysqli() functions instead of mysql() because mysql() function series are being departed from php . Also you missed some <tr> and </tr> s. so here is your fixed code:

<?php $num_rows = mysqli_num_rows($result);
echo $num_rows;
if($num_rows > 0){
$i = 0;
$dyn_table = '<table class="gridData">';
$dyn_table .= '<tr><th>Mil Purchy</th><th>Next Mil Purchy</th><th>total Missing</th><th>Missing From-To</th>';
        while($row = mysqli_fetch_array($result))
            {
                if ($i % 1 == 0) { // if $i is divisible by our target number (in this case "3")                           
                                     echo "<script> alert('$i') </script>";
                                     $dyn_table .= '</tr><tr><td>' .$row[0] . '</td>';
                                     $dyn_table .= '<td>' .$row[1]. '</td>';
                                     $dyn_table .= '<td>' .$row[2]. '</td>';
                                     $dyn_table .= '<td>' .$row[3]. '</td>';
                                 } else {
                                     echo "<script> alert ('in else statement') </script>";
                                 }
                                 $i++;
            }
$dyn_table .= '</tr></table>';
echo $dyn_table;
}?>

Also you are creating many <script> inside the <table> structure code, it is not wrong and it works but it is bad HTML so I recommend that you store all <script> s in an array and after creation of table, print out the array. any by the way, why sending many alerts? why not only print the alerts only?

Database is returning correct records that is 12.

You made a mistake in the following line of code:-

for ( $x = 0; $x<= $num_rows; $x++){

Simply replace above code block with the following one:-

for ( $x = 0; $x < $num_rows; $x++){

Here you are trying to get ($num_rows + 1) = 13 records from the database that's why you are not getting data for last record that is 13.

You do not need a for loop. Your while loop will stop when there are no more records. That's why it's there.

Remove for loop and check it may be work.

$num_rows = mysql_num_rows($result);
echo $num_rows;
if ($num_rows > 0) 
{
   $i = 0;
   $dyn_table = '<table class="gridData">';
   $dyn_table.= '<th>Mil Purchy</th><th>Next Mil Purchy</th><th>total Missing</th><th>Missing From-To</th>';
   while ($row = mysql_fetch_array($result)) {
   if ($i % 1 == 0) 
   {
      // if $i is divisible by our target number (in this case "3")
       echo "<script> alert('$i') </script>";
       $dyn_table.= '<tr><td>' . $row[0] . '</td>';
       $dyn_table.= '<td>' . $row[1] . '</td>';
       $dyn_table.= '<td>' . $row[2] . '</td>';
       $dyn_table.= '<td>' . $row[3] . '</td>';
    } 
   else 
   {
       echo "<script> alert ('in else statement') </script>";
   }
   $i++;
 }
 $dyn_table.= '</tr></table>';
echo $dyn_table;
}

13 will not divisible by 3 so it only shows 12 results

      <?php

$num_rows = mysql_num_rows($result);
echo $num_rows;
if ($num_rows > 0) {
    $i = 0;
    $dyn_table = '<table class="gridData">';
    $dyn_table .= '<th>Mil Purchy</th>
    <th>Next Mil Purchy</th>
    <th>total Missing</th>
    <th>Missing From-To</th>';
    while ($row = mysql_fetch_array($result)) {
        if ($i % 1 == 0) { // if $i is divisible by our target number (in this case "3")  
            echo "<script> alert('$i') </script>";
            $dyn_table .= '<tr><td>' . $row[0] . '</td>';
            $dyn_table .= '<td>' . $row[1] . '</td>';
            $dyn_table .= '<td>' . $row[2] . '</td>';
            $dyn_table .= '<td>' . $row[3] . '</td>';
        } else {
            echo "<script> alert ('in else statement') </script>";
        }
        $i++;
    }
    $dyn_table .= '</tr></table>';

    echo $dyn_table;
}?>

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