PHP 搜索 MySQL 数据库 - 多个搜索变量

标签 php mysql search

对于所有关心的人,if not empty 语句对我有用

我是 SQL 和 PHP 新手,正在尝试实现搜索功能以从处理 Wine 的 MySQL 数据库中获取数据。

我已经弄清楚如何在有一个搜索变量的情况下进行查询,并且我已经弄清楚如何使用两个搜索变量进行查询,我确信我可以继续这种模式 - 但是我是什么想要做的是实现一个搜索功能,可以根据用户输入到变量中的内容进行搜索(这意味着用户必须至少输入一个值,并且搜索将抓取与搜索变量相关的字段)。

假设我有这些搜索变量:

  • 酒名 -(用户可以将此留空或输入一个值)
  • Wine 类型 -(用户输入值)
  • 年份 -(用户可以将此留空或输入一个值)

根据用户输入的变量数量来决定搜索的精确程度。

我尝试搜索论坛,但似乎找不到任何内容。如果我的格式或问题有误,请道歉。希望您能提供任何帮助或指出正确的方向,谢谢!

这是迄今为止我的代码,如果用户输入变量“wineName”和“wineryName”,则该代码有效。尝试使用 isset 触发某种开关,但我认为我没有走在正确的轨道上。

<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html401/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>Answer Page</title>
</head>
<body bgcolor="white">

<?php 

    function showerror() {
        die("Error " . mysql_errno() . " : " . mysql_error());
    }

    require 'db.php';

    // Show all wines in a region in a <table>
    function displayWines($connection, $query, $wineName) {
    // Run the query on the server
        if (!($result = @ mysql_query ($query, $connection))) {
            showerror();
        }

    // Find out how many rows are available
        $rowsFound = @ mysql_num_rows($result);

        // If the query has results ...
        if ($rowsFound > 0) {
            // ... print out a header
            print "You searched for $wineName with a region of $wineryName <br><br>";

            // and start a <table>.
            print "\n<table>\n<tr>" .
                "\n\t<th>Wine ID</th>" .
                "\n\t<th>Wine Name</th>" .
                "\n\t<th>Winery Name</th>" . 
                "\n\t<th>Year</th>\n</tr>"; 

            // Fetch each of the query rows
            while ($row = @ mysql_fetch_array($result)) {
            // Print one row of results
            print "\n<tr>\n\t<td>{$row["wine_id"]}</td>" .
                "\n\t<td>{$row["wine_name"]}</td>" .
                "\n\t<td>{$row["winery_name"]}</td>" .
                "\n\t<td>{$row["year"]}</td>\n</tr>"; 
            } //end while loop body

            //finish table 
            print "\n</table>"; 
        } //end if $rowsFound body 

        //Report how many rows were found
        print "<br>{$rowsFound} records found matching your criteria<br>"; 
    } //end of function

    // Connect to the MySQL server
    if (!($connection = @ mysql_connect(DB_HOST, DB_USER, DB_PW))) {
        die("Could not connect");
    }

    //get user data 
    $wineName = $_GET['wineName']; 
    $wineryName = $_GET['wineryName'];

    if (!mysql_select_db(DB_NAME, $connection)) {
        showerror();
    }

    //start a query 
    $query = "SELECT wine_id, wine_name, winery_name, year 
    FROM wine, winery 
    WHERE wine.winery_id = winery.winery_id"; 

    if (isset($wineName)) {
        $query .= " AND wine_name = '{$wineName}'";
    }

    if (isset($wineryName)) {
        $query .= " AND winery_name = '{$wineryName}'";
    }

    //order the list 
    $query .= " ORDER BY wine_name"; 

    //run query, show results 
    displayWines($connection, $query, $wineName); 

?>

</body>
</html>

最佳答案

//start a query 
$query = "SELECT wine_id, wine_name, winery_name, year 
FROM wine, winery 
WHERE wine.winery_id = winery.winery_id"; 

if (isset($wineName)) {
    $query .= " AND wine_name LIKE '%$wineName%'";
}

if (isset($wineryName)) {
    $query .= " AND winery_name LIKE '%$wineryName%'";
}

//order the list 
$query .= " ORDER BY wine_name"; 

//run query, show results 
displayWines($connection, $query, $wineName); 

关于PHP 搜索 MySQL 数据库 - 多个搜索变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25285164/

相关文章:

MYSQL 在插入查询中使用多个选择返回列数与第 1 行的值数不匹配

mysql - 寻找不区分大小写的 MySQL 排序规则,其中 "a"!= "ä"

mysql - 行作为带有查找表的列

ruby-on-rails - Twitter API- Rails 未定义方法 `name' 为 nil :NilClass

php - 仅使用 PHP 对搜索结果进行分页

javascript - 如何修复这个程序,让它只搜索我的名字?

php - Laravel 4:PHP fatal error :调用未定义的方法 Blueprint::int()

php - 如何在生产服务器中安装 php memcache?

php : simple azure login with info

javascript - PHP/CSS 如果页面在手机上加载,则仅显示 JPG 背景