用键入的字母显示歌曲

时间:2014-02-15 11:41:04

标签: php html database

我正在根据数据库的建议制作一个搜索表单。一切都很顺利,但问题是当我输入“t”时,它会显示带有t的歌曲同样显示带有s字母的歌曲,这里也是我的代码:

<?php
include('config.php');
if($_POST) {
    $q = $_POST['searchword'];

    $sql_res=mysql_query("select * from vass_songs");
    while($row=mysql_fetch_array($sql_res)) {
        $id=$row['id'];
        $title=$row['title'];
        $img=$row['img'];
        $album_id=$row['album_id'];
        $re_title='<b>'.$q.'</b>';
?>
        <div class="suggestion_box" align="left">
            <a href="/song/<?php echo $id; ?>">
                <img src="/static/albums/<?php echo $id; ?   >_small.jpg");">
                </div><?php echo $title; ?><br/>
                <span style="font-size:9px; color:#999999"><?php echo $country; ?></span>
            </a>
        </div>
<?php
    }
} else {

}
?>

1 个答案:

答案 0 :(得分:1)

试试这个:

<?php
include('config.php');

if(isset($_POST['searchword'])) {
    $q = $_POST['searchword'];

    $sql_res=mysql_query("
        SELECT *
        FROM vass_songs
        WHERE title LIKE '%$q%'
    ");

    while($row=mysql_fetch_array($sql_res)) {
        $id     = $row['id'];
        $title  = $row['title'];
        $img    = $row['img'];
        $album_id = $row['album_id'];
        $re_title = "<b>{$q}</b>";

        ?>
        <div class="suggestion_box" align="left">
            <a href="/song/<?php echo $id; ?>">
                <img src="/static/albums/<?php echo $id; ?>_small.jpg");">
                <div><?php echo $title; ?></div><br/>
                <span style="font-size:9px; color:#999999">
                    <?php echo $country; ?>
                </span>
            </a>
        </div>
        <?php
    }
} else {

}
?>