1

在我正在制作的网站中,我需要一个搜索引擎来查找歌曲供人们收听。我让它可以从数据库中获取信息并将它们显示在页面上。当有两首歌曲同名时,问题就来了。我有一个系统,因此结果将转到单独的链接,但是当我搜索它们时,即使它们有两个单独的来源,它们也会显示相同的图像。由于某种原因,它也会产生额外的结果。这是我的代码:

<?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.";
    }
}
?>
4

2 回答 2

2

while($row = mysql_fetch_array($result))尝试$result['title']代替$row['title'];。同样适用于$url$arturl

这应该有效。

if ($numrows != 0) {
    $index = 0;
    $results = array();
    while($row = mysql_fetch_array($query)) {
            $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 />              
                ";
            }
    }           
    }
于 2013-01-18T02:19:13.080 回答
0

您的代码似乎正确。但是,为了澄清,请尝试回显 $arturl 并查看它是否获得了正确的源名称。并检查数据库中的值。最后一件事是尝试检查源图像是否在正确的文件夹中。尝试并提供反馈。

于 2013-01-18T02:02:38.470 回答