php - mysql_fetch_row 循环不返回第一组结果

标签 php mysql

我试图返回一组由开始和结束 session 变量决定的值。该代码可以工作,但由于某种原因不会从初始变量返回任何内容。

例如如果输入范围 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"); } }?>

最佳答案

正在运行的查询包含此子句:

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

但是 BETWEEN 正在进行字符串比较,而不是 LIKE。按字母顺序排列,ABC001 出现在 ABC001% 之前,因此您期望的第一行与查询不匹配。

关于php - mysql_fetch_row 循环不返回第一组结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19518881/

相关文章:

java - 我无法在我的类中将时区转换器实现为函数,即使它在我的主类中工作

php - 意外> T_STRING,期望为T_OLD_FUNCTION或> T_FUNCTION或T_VAR或 '}'

php - 使用 jquery ajax 和 php 实现过滤内容

PHP 运行脚本,完成后重定向

php - 无法在 MySQL 中插入度数符号

java - hibernate :分组非常慢

php - 存储和检索 URL 列表的最佳方法

php自动对象到数组的转换

php - bindParam()、bindValue()和execute(array())有什么区别和优势

php - Yii2:自定义登录失败消息并提供确切原因