MySQL总是在循环中使用错误的ID

时间:2015-09-19 11:32:35

标签: php mysql

我的while循环有问题。很难解释但是count函数总是返回1,也应该返回0 ...

这是我的代码:

$query = mysql_query("select SongID, Songtitel, artwork, duration, Upvote, SCID from tMusic order by Songtitel ASC") or die(mysql_error());
        $activeUserID = $_SESSION["user_id"];
        while ($temp = mysql_fetch_array($query)) {
        if (strlen($temp['Songtitel']) >= 47) {
            $ifToLong = "...";   
        } else {
            $ifToLong = "";   
        }
        $activeSongID = $temp['SongID'];
        $fistquery = mysql_query("select * from tMusicFists where SongID = ".$activeSongID." and UserID = ".$activeUserID."") or die(mysql_error());
        echo '<div class="songdiv" onclick="StreamStart('.$temp['SCID'].'); playTrack(); changeSongtitle('.$temp['Songtitel'].');">';
        echo '<div class="songimgdiv"><img class="songimg" src="'.$temp['artwork'].'" alt=""></div>';
        echo '<div class="songinfodiv">';
        echo '<div class="songtitleinfodiv"><p class="songtitleinfo">'.$activeSongID.'  '.$activeUserID.'  '.count($fistquery).'</p></div>';
        echo '<div class="songdurationdiv"><p class="durationcount">';
        $input = $temp[duration];
        $uSec = $input % 1000;
        $input = floor($input / 1000);
        $seconds = $input % 60;
        $input = floor($input / 60);
        $minutes = $input % 60;
        $input = floor($input / 60);
        echo $minutes.':'.$seconds;
        echo '</p></div>';
        if(count($fistquery) > 0) {
                echo '<div class="songratingdiv"><img class="thumbupimg" src="../images/likebrofist.png" alt=""><p class="thumbupnumber">'.$temp['Upvote'].'</p></div>';
            } else {
                echo '<div class="songratingdiv"><img class="thumbupimg" src="../images/brofist.png" alt=""><p class="thumbupnumber">'.$temp['Upvote'].'</p></div>'; 
            }

        echo '</div>';
        echo '</div>';
        }
        mysql_close();

我知道我不应该使用mysql函数,但我必须这样尝试......

谢谢

3 个答案:

答案 0 :(得分:0)

$fistquery = mysql_query("select * from tMusicFists where SongID = ".$activeSongID." and UserID = ".$activeUserID."") or die(mysql_error());

count($fistquery);

不行......请尝试使用mysql_num_rows($ firstquery);

答案 1 :(得分:0)

您需要使用mysql_num_rows而不是count

请使用以下脚本

$num_rows = mysql_num_rows($fistquery);
if($num_rows  > 0) {

而不是

if(count($fistquery) > 0) {

答案 2 :(得分:0)

mysql_query返回资源或某个布尔值。 无论如何,count资源或布尔值是1

如果您需要行数 - 请使用mysql_num_rows