使用内部连接在php和mysql中搜索

vojdkbi0  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(221)

我不是一个后端开发人员,而是一个前端开发人员,但是我正在尝试创建一个简单的php搜索,我已经遵循了这里的教程,并对它的不推荐位进行了更改。我有一个包含子行和子列的数据库,我正在尝试从中获取一些数据:
首先,我创建了html

<form action="search.php" method="GET">
        <input type="text" name="query" />
        <input type="submit" value="Search" />
      </form>

然后是php位:

$con = mysqli_connect("localhost", "USER", "PSW", "DBNAME") or die("Error connecting to database: ".mysqli_error());
$query = $_GET['query']; 

$min_length = 3;

if(strlen($query) >= $min_length){

    $query = htmlspecialchars($query); 

    $query = mysqli_real_escape_string($con, $query);

    $raw_results = mysqli_query($con, "SELECT * FROM article
        WHERE (`title` LIKE '%".$query."%') ") or die(mysqli_error($con));

    if(mysqli_num_rows($raw_results) > 0){

        while($results = mysqli_fetch_array($raw_results, MYSQLI_ASSOC)){
            echo "<p><h3>".$results['page_id']."</h3>".$results['title']."</p><p>".$results['space']."</p>";
        }

    }
    else{
        echo "No results";
    }

}
else{
    echo "Minimum length is ".$min_length;
}

上面是罚款,它给我的文章标题和文章id我在我的数据库。但我需要得到 space 以及 year . 附db结构:

以及

通过四处阅读并查看我的后端开发人员当时所做的工作,我可以在其他地方看到这一行,如果它有帮助的话,应该会走上正确的轨道。

$query = "SELECT A.id AS article_id, A.title AS title, C.name as space, A.page_id, T.year, T.month, T.id as time_id FROM article AS A INNER JOIN space AS S ON S.article_id = A.id INNER JOIN country AS C ON C.id = S.country_id INNER JOIN time AS T ON A.id = T.article_id WHERE (T.year BETWEEN ".$year_start." AND ".$year_stop.") GROUP BY A.title, C.name";
qv7cva1a

qv7cva1a1#

代替

SELECT * FROM article
    WHERE (`title` LIKE '%".$query."%')

用这个

SELECT s.original_name as location, a.title, a.page_id, t.year from space s,article a, time t where a.id = s.article_id and s.article_id = t.article_id and a.title LIKE '%".$query."%'

此查询将帮助您显示php中所需的列。

相关问题