PHP:在表中显示mysql数据库中的数据

时间:2015-01-20 09:02:49

标签: php mysql html5

我是php新手并尝试从数据库中获取数据并尝试在html表中显示它。我的问题是返回记录的总数是13,但是在表中它只显示12条记录(它在html表中跳过第一条记录)我的代码snippt如下所示

$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;
}

并在提交按钮上我写了以下代码

<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>

5 个答案:

答案 0 :(得分:1)

在使用mysqli()函数而不是mysql()之前,重要的是因为mysql()函数系列正在离开php。你也错过了一些<tr></tr>。所以这是你的固定代码:

<?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;
}?>

此外,您在<script>结构代码中创建了许多<table>,它没有错,但是它很有效HTML因此我建议您存储所有<script>array中,在创建表之后,打印出数组。顺便问一下,为什么发送许多警报?为什么不只打印警报?

答案 1 :(得分:0)

数据库正在返回12的正确记录。

您在以下代码行中犯​​了一个错误: -

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

只需使用以下代码块替换上面的代码块: -

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

在这里,您试图从数据库中获取($ num_rows + 1)= 13条记录,这就是为什么您没有获取13的最后一条记录的数据。

答案 2 :(得分:0)

您不需要for循环。当没有更多记录时,您的while循环将停止。这就是它存在的原因。

答案 3 :(得分:0)

删除for循环并检查它是否可行。

$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;
}

答案 4 :(得分:-1)

13将不会被3整除,因此它只显示12个结果

      <?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;
}?>