查询不适用于WHERE和仅与WHERE一起使用

时间:2012-11-07 20:21:40

标签: php mysql

标题的单个查询有效,但存在多个标题的歌曲。所以包含艺术家的新查询不起作用。虽然EXACT搜索歌曲标题是正确的,但可以使用单一查询。

我做错了什么

            //$checkTitle = mysql_query("SELECT * from lyrics WHERE title = '$querytitle'");
            $checkTitle = mysql_query("SELECT * FROM lyrics WHERE artist = '$queryArtist' AND title = '$queryTitle'");
            $result = mysql_fetch_array($checkTitle);
            if (!$checkTitle) {
                die('Query Failed');
            } 
            if ($result['title'] == $querytitle)  {
            ?><div id='message'>Song Titled Already Exist <?php echo $cleanTitle; ?> by Artist : <? echo $artist; ?></div><br><br><?
            } else {
            ?><div id="message">Song Titled : <?php echo $cleanTitle; ?> has been added to the database! - <a href="<?php echo $siteURL; ?>artist.php?aid=<?php echo $artist; ?>" target="_Blank">View Artist Page</a></div><br /><br /> <?php echo $queryArtist; ?><?
            print_r($result); // the data returned from the query
            }

3 个答案:

答案 0 :(得分:0)

SELECT * from lyrics WHERE artist = '$artist' AND title = '$title'

..或类似的东西..

答案 1 :(得分:0)

我看到你从未发布的其他问题中发现过这一点。尝试使用此查询:

SELECT * FROM lyrics WHERE artist = '$artist' AND title = '$queryTitle'

然后,如果您只期望一个结果,您可以使用这样的东西来利用它:

$query = "SELECT * FROM lyrics WHERE artist = '$artist' AND title = '$queryTitle'";
$checkTitle = mysql_query($query);
// assuming you have one result:
$row = mysql_fetch_array($checkTitle)
if ($row['title'] == $querytitle) {
    echo "<div id='message'>Song Titled Already Exist $cleanTitle by Artist : $cleanTitle</div><br><br>";
}

答案 2 :(得分:0)

正如评论所提到的,这非常低效,因为数据库可以更快,更简单地执行此操作:

SELECT * from lyrics WHERE artist = '$artist' AND title = '$queryTitle'

您可以通过确保(艺术家,标题)上有索引来进一步改善这一点。

另外,在运行此查询之前,请确保mysql_real_escape_string $artist$queryTitle {{1}}以保护自己免受SQL injection的攻击。<​​/ p>