为什么我的查询结果不完整


Why is my query result not complete?

我用php制作了一个应用程序,用TCPDF制作了一份报告,没有问题,select查询运行正常。但如果我仔细查看数据,我会发现第一个数据不见了。

简单示例查询:

 SELECT * FROM tb_expedition

如果我在MySQL上运行这个查询,所有的数据都会出现。如果查询是从我的php程序中运行的,那么结果仍然不完整。如果总数据为20行,则php中只显示19行,并且缺少第一个数据。

这是我的代码:

<?php
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// create new PDF document
 include "koneksi.php";//conection file
  $period=$_GET[PERIOD];
   $sql = "SELECT * FROM tb_exp_expor where period = ' $period'";
   $hasil = mysql_query($sql);
   $data = mysql_fetch_array($hasil);
   $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
   // set font
      $pdf->SetFont('times', '', 11);
// landscape
   $pdf->addPage( 'L', 'LETTER' );
   $html = '
<table border="1" cellspacing="3" cellpadding="4">
    <tr><td colspan="9" align="center"><h2>Form Pantauan Expedisi Export</h2></td></tr>
    <tr>
        <th align="center"><b>Tanggal</b></th>
        <th align="center"><b>Nama Expedisi</b></th>
        <th align="center"><b>Nama Distributor</b></th>
        <th align="center"><b>Kota Tujuan</b></th>
        <th align="center"><b>No Faktur</b></th>
        <th align="center"><b>Kondisi Armada Pengiriman</b></th>
        <th align="center"><b>Ketepatan Jumlah</b></th>
        <th align="center"><b>Ketepatan Waktu Kirim</b></th>
        <th align="center"><b>Keterangan</b></th>
    </tr></table>';
     $no=0;
   while ($data = mysql_fetch_array($hasil))
   {
     $html .= '<table border="1"><tr><td align="center">'.$data['tgl'].'</td>
                   <td align="center">'.$data['nama_exp_expor'].'</td>
                   <td align="center">'.$data['nama_distributor'].'</td>
                   <td align="center">'.$data['kota_tujuan'].'</td>
                   <td align="center">'.$data['no_faktur'].'</td>
                   <td align="center">'.$data['kon_armada'].'</td>
                   <td align="center">'.$data['ketepatan_jml'].'</td>
                   <td align="center">'.$data['ketepatan_wkt_kirim'].'</td> 
                   <td align="center">'.$data['ket'].'</td>  
                   </tr></table> ';
    $no++;             
    }
   $pdf->writeHTML($html, true, false, true, false, '');

   $pdf->Output('Daftar_Expedisi_Local', 'I');
?>

这些是您的一些线路,其中一些回波已被去除

 $query = mysql_query($sql);
 $data = mysql_fetch_array($sql);
 while($data = mysql_fetch_array($query))

第二行真的很奇怪:您做了一个额外的fetch_array,但使用sql字符串作为参数?这真的没有用,难道这不是一个错误吗?

您应该清理其中的一些代码,例如,不要执行您不需要的额外"fetch_array"。如果在那之后它不起作用,请删除所有不需要调试的不可见线(回声、颜色等)。你可以更快地了解发生了什么:)

当您在研究它时,您应该真正考虑从mysql_函数转移。请阅读手册中的警告http://php.net/manual/en/function.mysql-fetch-array.php

建议的替代

不鼓励使用此扩展。相反,MySQLi或PDO_MySQL应使用扩展名。另请参阅MySQL:选择API指南和有关更多信息的常见问题解答。此功能的替代方案包括:

mysqli_fetch_array()
PDOS语句::fetch()

现在请试试这个。这是作为一个"尽可能小"的例子,你首先应该做的事情。

<?php
  include "koneksi.php";//conection file
  $period=$_GET[PERIOD];  
  $sql = "SELECT * FROM tb_exp_expor where period = ' $period'";
  var_dump($sql);//you can check this query in your database manually
  $hasil = mysql_query($sql);       
  //NO FETCH ARRAY HERE!
   while ($data = mysql_fetch_array($hasil)){
        var_dump($data);
    }
?>

请注意:

  1. 移除额外的获取数组
  2. 删除所有用于调试的HTML和PDF内容
  3. 小型,尽可能多的自包含代码