2012-11-07 102 views
0

對標題作品的單個查詢存在但存在多個帶​​有標題的歌曲。所以包含藝術家的新查詢不起作用。儘管對歌曲標題的精確搜索是正確的,並且適用於單個查詢。查詢不適用於WHERE且僅適用於WHERE

我在做什麼錯

  //$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 
      } 
+4

看起來你應該在db查詢中做匹配 – 2012-11-07 20:24:03

+0

爲什麼你不在SQL查詢中這樣做? –

+2

你爲什麼不在你的查詢中的where子句中做這件事?想想如果你有一百萬條記錄,這是多麼的低效,你想要的標題是數據庫中的最後一條記錄?你取1,000,000行,並扔掉999,999。我不瞭解你,但是當我去買雜貨時,我只買我需要的東西。我不買全店,開車回家,然後扔掉除我想要的巧克力棒之外的全部庫存。 –

回答

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>"; 
} 
0
SELECT * from lyrics WHERE artist = '$artist' AND title = '$title' 

..或者類似的東西..

0

由於意見提了,這是可怕的效率低,因爲數據庫可以做到這一點更快,更簡單:

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

您可以通過確保您有(藝術家,標題)的指標進一步改善這一點。

此外,在運行此查詢以保護自己免受SQL injection攻擊之前,請確保您的mysql_real_escape_string均爲$artist$queryTitle

相關問題