gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:56:59 25 4
gpt4 key购买 nike

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

目前代码如下:

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

}
}

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

最佳答案

您可能需要使用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);

希望这有帮助。

关于php - 从不同的数据库表获取搜索结果并显示它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23911615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com