php else 空搜索语句
php else statement for empty search
我有一个搜索功能,可以匹配数据库中的关键字并回显一些 html 但我不知道如何启用空搜索处理。我可以使用 else 语句还是必须重新定义相同的参数并使用 !isset for if not set?
<?php
$con = mysqli_connect("localhost", "database", "password", "table");
if (isset($_GET['search'])) {
$search_query = $_GET['search_query'];
global $con;
$get_item = "select * from database where keywords like '%$search_query%'";
$run_item = mysqli_query($con, $get_item);
while ($row_item = mysqli_fetch_array($run_item)) {
$item_keywords = $row_item['item_keywords'];
echo "Search found for $search_query";
} // working fine up to here
} else {
echo "Search not found for $search_query";
}
?>
您可以像这样设置空搜索的条件。
if (isset($_GET['search']) && trim($_GET['search']) !='') { ... }
将您的条件更改为:
if (isset($_GET['search']) && trim($_GET['search_query']) != '') {
}
修改你的if条件
if (isset($_GET['search']) && !empty($_GET['search_query'])) {
}
您可以使用 empty
函数来完成:
if (!empty($_GET['search'])) {
//your code
}
您可以使用 isset()
和 mysqli_num_rows()
来检查空结果。并使用 mysqli_real_escape_string
来触发您的查询
if (isset($_GET['search'])) {
if (isset($_GET['search_query']) && $_GET['search_query'] != "") {/// check variable is set or not
$search_query = $_GET['search_query'];
$$search_query = mysqli_real_escape_string($con, $search_query);//
$get_item = "select * from `database` where `keywords` like '%$search_query%'";
$run_item = mysqli_query($con, $get_item);
$row_cnt = mysqli_num_rows($run_item); // count number of rows
if ($row_cnt > 0) {
while ($row_item = mysqli_fetch_array($run_item)) {
$item_keywords = $row_item['item_keywords'];
echo "Search found for $item_keywords";
}
} else {
echo "Search not found for $item_keywords";
}
} else {
echo "Search not found for $item_keywords";
}
}
我有一个搜索功能,可以匹配数据库中的关键字并回显一些 html 但我不知道如何启用空搜索处理。我可以使用 else 语句还是必须重新定义相同的参数并使用 !isset for if not set?
<?php
$con = mysqli_connect("localhost", "database", "password", "table");
if (isset($_GET['search'])) {
$search_query = $_GET['search_query'];
global $con;
$get_item = "select * from database where keywords like '%$search_query%'";
$run_item = mysqli_query($con, $get_item);
while ($row_item = mysqli_fetch_array($run_item)) {
$item_keywords = $row_item['item_keywords'];
echo "Search found for $search_query";
} // working fine up to here
} else {
echo "Search not found for $search_query";
}
?>
您可以像这样设置空搜索的条件。
if (isset($_GET['search']) && trim($_GET['search']) !='') { ... }
将您的条件更改为:
if (isset($_GET['search']) && trim($_GET['search_query']) != '') {
}
修改你的if条件
if (isset($_GET['search']) && !empty($_GET['search_query'])) {
}
您可以使用 empty
函数来完成:
if (!empty($_GET['search'])) {
//your code
}
您可以使用 isset()
和 mysqli_num_rows()
来检查空结果。并使用 mysqli_real_escape_string
来触发您的查询
if (isset($_GET['search'])) {
if (isset($_GET['search_query']) && $_GET['search_query'] != "") {/// check variable is set or not
$search_query = $_GET['search_query'];
$$search_query = mysqli_real_escape_string($con, $search_query);//
$get_item = "select * from `database` where `keywords` like '%$search_query%'";
$run_item = mysqli_query($con, $get_item);
$row_cnt = mysqli_num_rows($run_item); // count number of rows
if ($row_cnt > 0) {
while ($row_item = mysqli_fetch_array($run_item)) {
$item_keywords = $row_item['item_keywords'];
echo "Search found for $item_keywords";
}
} else {
echo "Search not found for $item_keywords";
}
} else {
echo "Search not found for $item_keywords";
}
}