Question

I have an advanced search query which queries a database. The search works fine and prints the desired results when a user searches something that is IN the database.

I've set up a condition when if a user searches something and that something couldn't be found in the database, it displays a message that the record could not be found.

But it's not displaying the message I need it to. Instead, if it can't find the record, it prints an empty table with headings. This table is only supposed to be printed if something is found.

No if I swop the condition from >= -1 to just == -1 it displays the message I need it to when something couldn't be found even if that something is in the database.

I hope this makes sense.

Please see my code below.

<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>
Was it helpful?

Solution

As a general rule, odbc_num_rows() is not a reliable way to determine the number of rows returned by a SELECT query. As mentioned in the "Notes" section of the PHP documentation:

Note:

Using odbc_num_rows() to determine the number of rows available after a SELECT will return -1 with many drivers.

That is indeed the case with the Access ODBC driver.

Instead of using odbc_num_rows() you could check the result of the first odbc_fetch_row() to see if it is TRUE and, if so, proceed with dumping the data to the HTML table. If the first call to odbc_fetch_row() returns FALSE then no rows were retrieved and you can display your message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top