【问题标题】:Getting search results from different database tables and displaying them?从不同的数据库表中获取搜索结果并显示它们?
【发布时间】:2014-07-17 16:14:04
【问题描述】:

我正在尝试创建一个搜索功能,该功能将检索我们在网站上创建的不同海报的搜索结果。如果一个人正在搜索让我们说“狗”,那么它将显示与狗有关的海报。该网站将以海报的形式发布不同的活动。

目前的代码如下:

<?php

class Search
{
    public static $con;

    private $search = '';

    function __construct()
    { 
        self::$con = mysqli_connect('localhost', 'guest', 'guestpw', 'db_users');

        $this->search = mysqli_real_escape_string(self::$con, $_POST['search']);
    }

    if(isset($_POST['submit_search']))
    {

    $sql = mysqli_query($con, "SELECT * FROM Event WHERE eventNamn LIKE '%" . $search);

    $sql = mysqli_query($con, "SELECT * FROM Användare WHERE userName LIKE '%" . $search);

    $sql = mysqli_query($con, "SELECT * FROM Poster WHERE Kategori LIKE '%" . $search);

    $sql = mysqli_query($con, "SELECT * FROM EventTyp WHERE EventTyp LIKE '%" . $search);

    $result = mysqli_query($sql);

    }
}

我现在要做的是使用用户正在搜索的搜索词,然后显示与该词关联的事件。 非常感谢所有帮助!谢谢。

【问题讨论】:

  • 你每次都替换变量 $sql 所以只有最后一个会被执行
  • 首先,您一遍又一遍地重新分配 $sql 变量。然后搜索变量上的对象范围错误(您应该改用$this-&gt;search)。
  • @mplungjan 所说的,每次你给 $sql 赋予新的含义时,你都会覆盖它。因此,前一个将“消失”,因此当您创建第 4 个 $sql 时,只有一个“存在”。
  • 您在查询中的所有变量中都缺少单引号。
  • 考虑也使用准备好的语句——像这样直接构建查询字符串是不安全的。 ca3.php.net/manual/en/mysqli.prepare.php

标签: php html mysql sql search


【解决方案1】:

您可能想要使用UNIONUNION ALL 运算符。 SQL UNION 运算符将两个或多个 SELECT 语句的结果组合在一起。

 SELECT col FROM Event WHERE ...
 UNION ALL
 SELECT col FROM User WHERE ...

文档在这里:
MYSQL UNION 运算符:http://dev.mysql.com/doc/refman/5.0/en/union.html


你的代码可能是这样的:

$sql  = "SELECT [your column] AS event FROM Event WHERE eventNamn LIKE '%" . $search . "'".
    "UNION ALL ".
    "SELECT [your column] AS event FROM Användare WHERE userName LIKE '%" . $search . "'".
    "UNION ALL".
    "SELECT [your column] AS event FROM Poster WHERE Kategori LIKE '%" . $search . "'".
    "UNION ALL".
    "SELECT [your column] AS event FROM EventTyp WHERE EventTyp LIKE'%" . $search . "'";

$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result)) {
  echo $row['event '];
  echo "<br>";
}
mysqli_close($con);

希望这会有所帮助。

【讨论】:

  • 感谢您的回答。使用 UNION 或 UNION ALL 后如何显示搜索结果?以及如何解决替换 $sql 变量的问题?这解决了这个问题吗?
【解决方案2】:

您现在拥有的代码无法正常工作。您正在检查类中的 $_POST 请求吗?我做了一些调整

<?php

class Search
{
    public static $con;
    private $search = '';

    public function __construct($search)
    { 
        self::$con = mysqli_connect('localhost', 'guest', 'guestpw', 'db_users');

        $this->search = mysqli_real_escape_string(self::$con, $search);
    }

    //Do this for every search

    public function search() {
       $sql = mysqli_query(self::$con, "SELECT [column] FROM Event WHERE eventNamn LIKE '%" .       $search . "'".
"UNION ALL ".
"SELECT [column] FROM Användare WHERE userName LIKE '%" . $search . "'".
"UNION ALL".
"SELECT [column] FROM Poster WHERE Kategori LIKE '%" . $search . "'".
"UNION ALL".
"SELECT [column] FROM EventTyp WHERE EventTyp LIKE'%" . $search . "'");
       return mysqli_query($sql);
    } 
}

if(isset($_POST['search'])) {

  $searchClass = new Search($_POST['search']);
  $result = $searchClass->searchEvent();

}

【讨论】:

  • 感谢您的回答。你的意思是让我为每个查询创建一个函数吗?像公共函数searchName()、searchEvent()等?
  • 只有 3 个其他函数 searchAnvändare()、searchPoster() 和 searchEventTyp();
  • @Daan Super Globals 可用于任何范围,甚至是一个类。
  • @r3wt 是的,这不是我的意思。我的意思是它看起来不干净。
  • @Daan 哦,我同意,但这不是问题所在。 OP 还不了解 PHP,但至少他/她正在尝试。
【解决方案3】:

1.不要依赖mysqli转义字符串。使用准备好的语句。它更快,更简单。

2.您可以很容易地在一个查询中从多个表中进行选择

public $mysqli; //make sure your connection is available

public $result = NULL;//setup your result variable
public $search = NULL;
public function getresults() //pass your connection to the object
{
    $search = $_POST["search"]; //post is a super global. no need to pass it by reference

    $result = array();//create an array to store the search results in

    $mysqli = $this->mysqli;//define mysqli so you can access the property of your class object. 

    //construct mysqli in your __construct call, and return it.

    $stmt = $mysqli->prepare("SELECT column1,column2,column3,column4 
                                FROM Event,Användare,Poster,Eventyp 
                                WHERE eventNamn LIKE '% ?';");//prepare our query

    $stmt->bind_param('s', $this->search); 

    //bind our search parameters to the question mark in the query. 
    //if you have multiple querystrings, they must be bound in order.
    if($stmt->execute())//execute prepared statment
    {
        $stmt->bind_result($col1,$col2,$col3,$col4);//bind the selected column results to variables. 
        //these also must be bound in order
        if($stmt->num_rows > 0)
        {
            $result[] = array('type'=> 'success','result' => $stmt->num_rows, 'search string' => $search); //success!
            while($row = $stmt->fetch()) //never use get_result to fetch your statement. always use fetch.
            {
                $result[] = array('type' => 'result', 
                                  'column1' => $col1 ,
                                  'column2' => $col2 ,
                                  'column3' => $col3 , 
                                  'column4' => $col4); //store each result row in an array.
            }
            $stmt->close();//close the connection
        }else{
            $result[] = array('type'=> 'emptyresult','result' => 'No Results Found', 'search string' => $search); //No Results!
        }
    }else{
        $result[] = array('type'=> 'error','result' => 'Error with query', 'search string' => $search); //No Results!
    }
    return $result;//finally return our result for further processing
}

【讨论】:

    猜你喜欢
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多