我不是后端开发人员,而是前端,但我正在尝试创建一个简单的php搜索,我已经跟随tutorial on here并对已经弃用的位进行了更改。我有一个包含子行和列的数据库,我正在尝试从中获取一些数据:
首先是我创建了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
。附件是数据库结构:
通过阅读并查看我的后端开发人员所做的事情,我可以在其他地方看到这条线,如果它有帮助的话应该走上正轨。
$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";
答案 0 :(得分:1)
替换
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中所需的列。