我有一个查询数据库的高级搜索查询。搜索工作正常,并在用户搜索数据库中的内容时打印所需的结果。
我已经设置了一个条件,当用户搜索某些内容并且无法在数据库中找到某些内容时,它会显示一条消息,指出无法找到该记录。
但是它没有显示我需要它的消息。相反,如果它找不到记录,它会打印一个带有标题的空表。只有找到某些东西才能打印此表。
否如果我将条件从> = -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>
答案 0 :(得分:1)
作为一般规则,odbc_num_rows()
不是确定SELECT查询返回的行数的可靠方法。正如PHP documentation的“注释”部分所述:
注意:
使用odbc_num_rows()确定SELECT之后可用的行数将返回-1并包含许多驱动程序。
Access ODBC驱动程序确实如此。
您可以检查第一个odbc_num_rows()
的结果,看看它是否为TRUE,如果是,则继续将数据转储到HTML表,而不是使用odbc_fetch_row()
。如果第一次调用odbc_fetch_row()
返回FALSE,则不会检索任何行,您可以显示消息。