【问题标题】:Display message if odbc_num_rows == empty如果 odbc_num_rows == 为空,则显示消息
【发布时间】:2014-04-07 10:46:38
【问题描述】:

我有一个高级搜索查询来查询数据库。当用户搜索数据库中的内容时,搜索工作正常并打印所需的结果。

我已经设置了一个条件,当用户搜索某些内容并且在数据库中找不到某些内容时,它会显示一条消息,指出找不到记录。

但它没有显示我需要的消息。相反,如果它找不到记录,它会打印一个带有标题的空表。仅当找到某些内容时才应该打印此表。

不,如果我将条件从 >= -1 转换为 == -1,它会在无法找到某些内容时显示我需要它的消息,即使该内容在数据库中也是如此。

我希望这是有道理的。

请看下面我的代码。

<table class="table table-bordered table-striped" style="width: 100%;"> 
    <?php

        $dbName = "F:/Domains/autodeal/autodeal.co.za/wwwroot/newsite/db/savvyautoweb.mdb";

        // Throws an error if the database cannot be found
        if (!file_exists($dbName)) {
            die("Could not find database file.");
        }

        // Connects to the database
        // Assumes there is no username or password
        $conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", '', '');

        $searchMake = addslashes($_POST['makeSelection']);
        $searchModel = addslashes($_POST['modelSelection']);
        $searchBranch = addslashes($_POST['branchSelection']);
        $searchYear = addslashes($_POST['yearSelection']);
        $minPrice = addslashes($_POST['minPriceSelection']);
        $maxPrice = addslashes($_POST['maxPriceSelection']);

        $sql = "SELECT Id, Make, Model, Year, Price, SpecialPrice, Branch, StockNO FROM Vehicle ";

        if ($searchMake || $searchModel || $searchBranch || $searchYear || $minPrice || $maxPrice) {
            $sql .= "WHERE ";
        }

        $combine = '';

        if ($minPrice) {
            $sql .="{$combine}Price BETWEEN $minPrice "; $combine = 'BETWEEN ';
        }

        if ($maxPrice) {
            $sql .="AND $maxPrice "; $combine = 'AND ';
        }

        if ($searchMake) {
            $sql .="{$combine}Make LIKE '%$searchMake%' "; $combine = 'AND ';
        }

        if ($searchModel) {
            $sql .="{$combine}Model LIKE '%$searchModel%' "; $combine = 'AND ';
        }

        if ($searchBranch) {
            $sql .="{$combine}Branch LIKE '%$searchBranch%' "; $combine = 'AND ';
        }

        if ($searchYear) {
            $sql .="{$combine}Year LIKE '%$searchYear%' "; $combine = 'AND ';
        }   

        $rs = odbc_exec($conn, $sql);

        if (odbc_num_rows($rs) >= -1) {

            echo "\t" . "<tr>\n";

            echo "\t" . "<th>Make</th><th>Model</th><th>Year</th><th>Price</th><th>Special Price</th><th>Location</th><th>Stock Number</th>" . "\n";

            while (odbc_fetch_row($rs)) { 
                $id = odbc_result($rs, Id);
                $make = odbc_result($rs, Make);
                $model = odbc_result($rs, Model);
                $year = odbc_result($rs, Year);
                $price = odbc_result($rs, Price);
                $specialPrice = odbc_result($rs, SpecialPrice);
                $branch = odbc_result($rs, Branch);
                $stockNo = odbc_result($rs, StockNO);

                echo "\t" . "<tr>\n";
                echo "\t\t" . "<td><a href=/newsite/selected-vehicles?Id=$id>" . $make . "</td><td><a href=/newsite/selected-vehicles?Id=$id>" . $model . "</a></td><td>" . $year . "</td><td>" . $price . "</td><td>" . $specialPrice . "</td><td>" . $branch . "</td><td>" . $stockNo . "</td>\n";

                echo "\t" . "</tr>\n";
            }

        } else {
            echo "We don’t have the vehicle you are looking for right now, but send us your vehicle requirements and we will be sure to find you one!";
        }

      odbc_free_result($rs);
      odbc_close($conn);

      // This message is displayed if the query has an error in it
      if (!$rs) {
          exit("There is an error in the SQL!");
      }

  ?>

</table>

【问题讨论】:

  • 应该是if (odbc_num_rows($rs) &gt; 0 ) 吗?
  • 如果我将其更改为 > 0,即使记录在数据库中,它也会显示所需的消息。如果我将其更改为

标签: php sql ms-access odbc


【解决方案1】:

作为一般规则,odbc_num_rows() 不是确定 SELECT 查询返回的行数的可靠方法。如 PHP documentation 的“注释”部分所述:

注意:

使用 odbc_num_rows() 来确定 SELECT 后可用的行数将返回 -1 与许多驱动程序。

Access ODBC 驱动程序确实如此。

您可以检查第一个odbc_fetch_row() 的结果,而不是使用odbc_num_rows(),看看它是否为TRUE,如果是,则继续将数据转储到HTML 表中。如果第一次调用 odbc_fetch_row() 返回 FALSE,则没有检索到任何行,您可以显示您的消息。

【讨论】:

    猜你喜欢
    • 2017-09-28
    • 1970-01-01
    • 2015-12-20
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    相关资源
    最近更新 更多