代码运行5次。它应该只运行一次。 (php,mysql)

时间:2013-09-26 11:24:58

标签: php mysql

此代码应显示数据库中的图像。它还创建了一个随机的背景颜色,工作正常。代码是一个函数,函数发布在index.php中,基本上是html。当我把代码直接放在index.html中时,函数和第一个while循环运行就好了。

    $query = mysql_query("SELECT `image_id`, `name`, `likes` FROM `images`");
    while(($row = mysql_fetch_assoc($query)) !== false){
        $sql = mysql_query("SELECT * FROM images");
        while($row = mysql_fetch_array($sql)){

        $images_id = $row["image_id"];
            $images_path = $row["path"];
            $images_name = $row["name"];
            $images_type = $row["type"];
            $images_size = $row["size"];
            $images_likes = $row["likes"];

            if($images_id == $images_id){   

            $kiesGetal = rand(1, 6);
                    switch ($kiesGetal){
                        case 1: $randKleur = "#a4a6a2";
                        break;

                        case 2: $randKleur = "#7e8661";
                        break;

                        case 3: $randKleur = "#9faac7";
                        break;

                        case 4: $randKleur = "#80957a";
                        break;

                        case 5: $randKleur = "#e09e6d";
                        break;

                        case 6: $randKleur = "#a0dd8d";
                        break;
                        }

        echo"<li style=\"background-color:$randKleur\" id=\"$images_id\" class=\"fotoFrame\">"
                     . "<p class=\"imagas_name\" id=\"$images_name\">$images_name</p>" 
                     . "<p>$images_likes mensen vinden dit leuk.</p>"        
                     . "<img class=\"images\" src='img/" . $row['name'] . "'></>" . "</li>";
            }else{
                    echo "error";
                }
        }
    }
            };
    ?> `

1 个答案:

答案 0 :(得分:1)

$query = mysql_query("SELECT `image_id`, `name`, `likes` FROM `images`");
while(($row = mysql_fetch_assoc($query)) !== false)

这些行不是必需的。