从不同的数据库表获取搜索结果并显示它们?

时间:2014-05-28 12:29:02

标签: php html mysql sql search

我正在尝试创建一个搜索功能,以便检索我们在网站上创建的不同海报的搜索结果。如果一个人正在寻找让我们说'#34; dog"然后它将显示与狗有关的海报。该网站将以海报的形式发布不同的活动。

目前代码如下:

<?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);

    }
}

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

3 个答案:

答案 0 :(得分: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);

希望这有帮助。

答案 1 :(得分:0)

您现在拥有的代码无法运行。您正在检查类中的$ _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();

}

答案 2 :(得分:0)

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
}