在我正在制作的网站中,我需要一个搜索引擎来查找歌曲供人们收听。我让它可以从数据库中获取信息并将它们显示在页面上。当有两首歌曲同名时,问题就来了。我有一个系统,因此结果将转到单独的链接,但是当我搜索它们时,即使它们有两个单独的来源,它们也会显示相同的图像。由于某种原因,它也会产生额外的结果。这是我的代码:
<?php
if (isset($_GET['q'])) {
$q = $_GET['q'];
mysql_connect('********', '********', '********');
mysql_select_db('********');
$query = mysql_query("SELECT * FROM ******** WHERE title LIKE '$q'");
$numrows = mysql_num_rows($query);
} else {
echo "
<span style='font-family: Helvetica, Arial;font-weight: bold;font-size: 25px;'>Search</span>
<form action='http://www.example.com/search' method='get'>
<input placeholder='Search for music' type='text' name='q' style='font-weight:bold;padding:5px;width:300px;border-top-left-radius: 4px;border-top-right-radius: 10px;border-bottom-left-radius: 10px;border-bottom-right-radius: 4px;border: 3px solid gray;background-color:#000000;color:#FFFFFF;' />
</form>
";
}
if ($numrows != 0) {
$index = 0;
$results = array();
while($row = mysql_fetch_assoc($query)) {
$results[$index] = $row;
$index++;
foreach ($results as $result) {
$url = "http://www.example.com?id=" . $row['id'];
$title = $row['title'];
$arturl = $row['art_url'];
if ($_GET['q'] != "") {
echo "
<a href='$url'>
<table>
<tr style='text-align:left;'>
<td><img src='$arturl' style='width:100px;height:100px;'></td>
<td>
<span class='songTitle'>$title</span>
<br/>
<span class='songArtist'>By: Unknown</span>
</td>
</tr>
</table>
</a>
<br />
";
}
}
}
} else {
if ($_GET['q'] != "") {
echo "
<span style='font-family: Helvetica, Arial;font-weight: bold;font-size: 25px;'>Search</span>
<form action='********' method='get'>
<input placeholder='Search for music' type='text' name='q' style='font-weight:bold;padding:5px;width:300px;border-top-left-radius: 4px;border-top-right-radius: 10px;border-bottom-left-radius: 10px;border-bottom-right-radius: 4px;border: 3px solid gray;background-color:#000000;color:#FFFFFF;' />
</form>
";
echo "<br />No results where found.";
}
}
?>