mysql_fetch_row循环不返回第一组结果

时间:2013-10-22 13:11:15

标签: php mysql

我正在尝试返回一组由开始和结束会话变量决定的值。代码有效但由于某种原因不会从初始变量返回任何内容。

e.g。如果输入ABC001到ABC010的范围,则返回ABC002 - ABC010的所有值,不显示ABC001。

我的查询在直接输入SQL时效果很好,我不认为我已经调用了结果集,为什么第一组结果不会返回?

PS我知道我使用过的很多功能已经/正在被弃用,但这在历史上是一个相当古老的网站,我还没有准备好重新编写它。

<?php
  require_once("includes/header2.php");
  $title = "Box Contents";
  require_once("includes/head2.php");
  isLoggedIn();

  // Catch session variables
  $_SESSION['boxidStart'] = $_POST['boxContentsStart'];
  $_SESSION['boxidEnd'] = $_POST['boxContentsEnd'];

  // Define them as named variables for ease of use
  $boxidStart = $_SESSION['boxidStart'];
  $boxidEnd = $_SESSION['boxidEnd'];

  // Box selection query
  $sql = mysql_query ("SELECT boxId, fileNumber, dateEntered, destroyDate, invoiceStatus, invoiceDate FROM fields WHERE boxId BETWEEN '$boxidStart%' AND '$boxidEnd%' ORDER BY boxId ASC");

  // SELECT Business
  $companyName = mysql_query("SELECT business from fields LEFT JOIN users ON fields.CompanyId = users.Id WHERE boxId LIKE '$boxidStart%'");
  $cn = mysql_fetch_row($companyName);
  $no = 1;

  if (!$boxidStart) {
  ?>
  <div class="clearfix"></div>
  <div>
  <h2>You need to enter some text to search for!</h2>
  <p>Please try again.</p>
  <?php
    require_once("includes/footer.php");
  } else {
    if (escape($boxidStart))
  {
  ?>
  <div class="clearfix"></div>
  <div>
  <h2 style="float:left;">Files Entered from range box <?php echo $boxidStart ?> to box <?php echo $boxidEnd ?></h2>
  <!-- company name -->
  <h2 style="float:right"><?php echo $cn[0] ?></h2>
  <div>
  <table class="results" cellpadding="3" cellspacing="0" border="1" width="100%">
    <tbody align="left">
    <tr>
        <th>No.</th>
        <th>Box No.</th>
        <th>File Number</th>
        <th>Date Entered</th>
        <th>Destroy Date</th>
        <th>Invoicing</th>
     </tr>
<?php

while ($row = mysql_fetch_row($sql)) {
    echo "<tr valign='middle' class='row'>
          <td width='5%' class='company'>", $no++ ,"</td>
          <td width='5%' class='company'>", $row[0] ,"</td>
          <td width='30%' class='company'>", $row[1],"</td>
          <td width='12.5%' class='datein'>", $new_date = date('d/m/Y', strtotime($row[2])), "</td>
          <td width='12.5%' class='filenotes'>", $new_date = date('d/m/Y', strtotime($row[3])), "</td>";

    // There must be a check in the history_files table to see if there is an entry for this fileNumber
    // If there is an entry for this file number then it means that the file has already been in the system and the invoice has been paid
    $sqlReturn = mysql_query ("SELECT fileNumber FROM history_files WHERE fileNumber = '$row[1]'");


    $result = mysql_fetch_array($sqlReturn);
    //var_dump($sqlReturn);
    if ($result) {
        // If the result is true the the text "Invoice has been paid"
        echo "<td width='35%' class='filenotes'>Invoice already paid</td></tr>";
    } else {
        // If the result is false the the text "Invoice Charge €25.00" plus VAT 
        echo "<td width='35%' class='filenotes'>Invoice charge €25.00 + VAT</td></tr>";
    }


    }
            //echo "<p>".mysql_num_rows($sql)." results<p>";
?>

</tbody>
</table>


<p><strong><br><br>Signed:</strong>________________________________<br><br></p>
<p><strong>Dated:</strong> <?php echo date('d/m/Y', strtotime($dateEntered)); ?></p>
<br><br>
</div>
<?php require_once("includes/foot.php"); } }?>

1 个答案:

答案 0 :(得分:1)

正在运行的查询包括以下子句:

BETWEEN '$boxidStart%' AND '$boxidEnd%'

但是BETWEEN正在进行字符串比较,而不是LIKE。按照alpahbetical顺序, ABC001  在 ABC001%之前,所以您期望的第一行与查询不匹配。