SELECT中的比较并不起作用

时间:2016-01-19 20:36:33

标签: php sql select psql

由于某种原因,以下代码(用于检查条目是否已存在):

$query = "SELECT 1 FROM Song WHERE EXISTS(SELECT *
              FROM Song WHERE SongTitle = '$songtitle'
              AND Duration = '$duration' AND Popularity = '$popularity'
              AND Description = '$description')";        
    $result = pg_query($conn,$query) or die("Query could not be executed");
    $row = pg_fetch_row($result);

没有正常工作,因为它在数据不同时找到了一对。这种情况特别适用于第五首歌(Dance V)。我做了很多测试,但我还没弄清楚。任何提示都会非常有用。

txt示例:

TITLE | ALBUM | ARTIST | DURATION | POPULARITY | DESCRIPTION
Dance I|Glass: Dance (Nos. 1-5)|Philip Glass|19:19|16|https://api.spotify.com/v1/tracks/4v2lPpMv4OXnYWc7Oed9Ax
Dance II|Glass: Dance (Nos. 1-5)|Philip Glass|22:58|10|https://api.spotify.com/v1/tracks/5aVwAZIBbO7OuUwzsXxnQT
Dance III|Glass: Dance (Nos. 1-5)|Philip Glass|18:35|11|https://api.spotify.com/v1/tracks/2rScuezUeKzAjeCqrBe0ZY
Dance IV|Glass: Dance (Nos. 1-5)|Philip Glass|23:31|8|https://api.spotify.com/v1/tracks/515LeLoiveW3VsoLzs3eln
Dance V|Glass: Dance (Nos. 1-5)|Philip Glass|20:14|9|https://api.spotify.com/v1/tracks/77xQaN6fCsy3ZRp1eBbyXq
Dance I|Glass: Dance (Nos. 1-5)|Philip Glass|19:19|0|https://api.spotify.com/v1/tracks/77YpPPHxnqi9VaetU7OXyA
Dance II|Glass: Dance (Nos. 1-5)|Philip Glass|22:58|0|https://api.spotify.com/v1/tracks/7veCJpGBXiwjf4nYF3MIBt
Dance III|Glass: Dance (Nos. 1-5)|Philip Glass|18:35|0|https://api.spotify.com/v1/tracks/766rgBXNJmlpNLNyObbPa6
Dance IV|Glass: Dance (Nos. 1-5)|Philip Glass|23:31|0|https://api.spotify.com/v1/tracks/3xhfqJfBT60JMHzxdijMIL
Dance V|Glass: Dance (Nos. 1-5)|Philip Glass|20:14|0|https://api.spotify.com/v1/tracks/2kKz8qwXIyvV9uZD2BnTjz

表歌曲信息:

CREATE TABLE song(
id SERIAL PRIMARY KEY,
songtitle varchar(100),
duration varchar(10),
popularity smallint,
description varchar(100)
);

代码:

while(!feof($myfile)) {
    $line = fgets($myfile);
    $token = strtok($line,"|");
    if(feof($myfile)) break; # extra
    while($token!=false) {
        $word_count++;
        if($word_count==1) {
            $songtitle = pg_escape_string($token);
        } else if($word_count==2) {
            $album = pg_escape_string($token);
        } else if($word_count==3) {
            $artist = $token;
        } else if($word_count==4) {
            $duration = $token;
        } else if($word_count==5) {
            $popularity = $token;
        } else {
            $description = $token;
        }
        $token = strtok("|");
    }
    $word_count=0;

    $query = "SELECT 1 FROM Song WHERE EXISTS(SELECT *
              FROM Song WHERE SongTitle = '$songtitle'
              AND Duration = '$duration' AND Popularity = '$popularity'
              AND Description = '$description')";         
    $result = pg_query($conn,$query) or die("Query could not be executed");
    $row = pg_fetch_row($result);
    if($row[0])
    {
        printf("\n");
        echo 'Entry already exists: ';
        printf("\n");
        echo 'songtitle: ';
        echo $songtitle;
        printf("\n");
        echo 'Description: ';
        echo $description;
        printf("\n");
    }
    else
    {
        $query = "INSERT INTO Song (songtitle, duration, popularity, description) VALUES ('$songtitle','$duration','$popularity','$description')";
        $result = pg_query($conn,$query) or die("Query could not be executed");
    }
}

输出:

postgres@user-ThinkCentre-A55:/var/www/html/songtest$ php -q add.php


Entry already exists: 
songtitle: Dance V
Description: https://api.spotify.com/v1/tracks/77xQaN6fCsy3ZRp1eBbyXq


postgres@user-ThinkCentre-A55:/var/www/html/foodtest$

1 个答案:

答案 0 :(得分:0)

这可能应该是一个评论,但为什么使用存在?

$query = "SELECT top 1 *
              FROM Song WHERE SongTitle = '$songtitle'
              AND Duration = '$duration' AND Popularity = '$popularity'
              AND Description = '$description'";     

如果你只有一行,请使用前1名。