脚本:
<?php
include("connect.php");
?>
<?php
echo "<h1>The Hashtag: </h1>";
if(isset($_GET['hashtag'])){
echo $_GET['hashtag'];
}
// Select the ID of the given hashtag from the "hashtags" table.
$tqs = "SELECT `id` FROM `hashtags` WHERE `hashtag` = '" . $_GET['hashtag'] . "'";
$tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));
$row = mysqli_fetch_assoc($tqr);
// This prints e.g.:
// 100
echo "<br/><br/>";
print_r($row['id']);
// Select the threads from the "thread" table which contains the hashtag ID number.
$tqs_two = "SELECT * FROM `thread` WHERE `hashtag_id` IN ('" . $row['id'] . "')";
$tqr_two = mysqli_query($dbc, $tqs_two) or die(mysqli_error($dbc));
$row_two = mysqli_fetch_assoc($tqr_two);
echo "<br/><br/>";
print_r($row_two);
?>
脚本应该根据标签的ID号来选择行。它应该在表的"hashtag_id"列中查找id号,如果可以在那里找到,那么它应该选择该行。
ID号在"hashtag_ID"列中,用逗号分隔。
示例:
98, 99, 100
基本上,有没有一种方法可以执行SQL查询,看看列"是否包含"该ID号,或者可能需要其他信息?
欢迎提出任何建议。
正如周所说,您可以使用"contains"。不过,你需要谨慎;如果您要查找"9"的hashtag_id,则查询需要避免返回"19"、"99"、"93"等。为此,您必须依赖该hashtag_ids字段中数据的确切格式。如果你的数字真的用逗号和空格分隔,你可以通过执行"hashtag_id LIKE'%,9,'"很容易地找到不在查询开始或结束时的精确匹配。然而,在hashtag_id字段的开头或结尾都找不到。要捕获这些,您还需要"hashtag_id LIKE'9,%'"answers"hashtag_ILIKE'%,9'"。
因此,要抓住所有三种可能性:
SELECT * FROM `thread` WHERE `hashtag_id` LIKE '9,%' or `hashtag_id` LIKE '%, 9,%' or `hashtag_id` LIKE '%, 9';
要执行包含,您将使用LIKE
-
$tqs_two = "SELECT * FROM `thread` WHERE `hashtag_id` LIKE '%" . $row['id'] . "%'";