输出多个记录php

时间:2014-01-29 04:41:48

标签: php mysql

好的,所以我想我只是设置我的MySQL表不好。我认为,其他无关紧要的事情,然后,movielink1,movielink2,movielink3,movielink4,movielink5

我想为每个按钮显示一个按钮,仅在movielink#存在时才显示。他们中的大多数只是填充了movielink1。

我想我只是以错误的方式看待它。这是我的代码。

             <?php 
            $data2 = mysql_query("SELECT movielink1,movielink2,movielink3,movielink4,movielink5 FROM movies WHERE id=$id");
            if (mysql_num_rows($data2) > 0 ) {
            while($info2 = mysql_fetch_array($data2)) {
                Print '<a href="'.$info['movielink1'];.'?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="'.$info['title'];.'"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
                }
            } else {
                Print 'Error!'; 
            }
            ?>

感谢您的建议,我刚刚补充了这一点,但它仍然无法正常工作,也不是一种很好的方法。

        $data1 = mysql_query("SELECT movielink1 FROM movies WHERE id=$id");
        if (mysql_num_rows($data1) > 0 ) {
        while($info1 = mysql_fetch_array($data1)) {
            Print '<a href="'.$info['movielink1'].'?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="'.$info['title'].'"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
            }
        } else {
        }

        $data2 = mysql_query("SELECT movielink2 FROM movies WHERE id=$id");
        if (mysql_num_rows($data2) > 0 ) {
        while($info2 = mysql_fetch_array($data2)) {
            Print '<a href="'.$info['movielink2'].'?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="'.$info['title'].'"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
            }
        } else {
        }
        $data3 = mysql_query("SELECT movielink3 FROM movies WHERE id=$id");
        if (mysql_num_rows($data3) > 0 ) {
        while($info3 = mysql_fetch_array($data3)) {
            Print '<a href="'.$info['movielink3'].'?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="'.$info['title'].'"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
            }
        } else {
        }
        $data4 = mysql_query("SELECT movielink4 FROM movies WHERE id=$id");
        if (mysql_num_rows($data4) > 0 ) {
        while($info4 = mysql_fetch_array($data4)) {
            Print '<a href="'.$info['movielink4'].'?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="'.$info['title'].'"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
            }
        } else {
        }
        $data5 = mysql_query("SELECT movielink5 FROM movies WHERE id=$id");
        if (mysql_num_rows($data5) > 0 ) {
        while($info5 = mysql_fetch_array($data5)) {
            Print '<a href="'.$info['movielink5'].'?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="'.$info['title'].'"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
            }
        } else {
        }

1 个答案:

答案 0 :(得分:1)

您的代码中存在许多小问题。例如,您从结果中获取行并将其设置为$info2,然后调用$info

以下是您修改的代码:

$sqlResults = mysql_query("SELECT * FROM movies WHERE id=$id");

if (mysql_num_rows($sqlResults) > 0 ) {
    while($sqlRow = mysql_fetch_array($sqlResults)) {
        if (!empty($sqlRow['movielink1'])) {
            print '<a href="' . $sqlRow['movielink1'] . '?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="' . $sqlRow['title'] . '"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
        }
        if (!empty($sqlRow['movielink2'])) {
            print '<a href="' . $sqlRow['movielink2'] . '?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="' . $sqlRow['title'] . '"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
        }
        if (!empty($sqlRow['movielink3'])) {
            print '<a href="' . $sqlRow['movielink3'] . '?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="' . $sqlRow['title'] . '"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
        }
        if (!empty($sqlRow['movielink4'])) {
            print '<a href="' . $sqlRow['movielink4'] . '?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="' . $sqlRow['title'] . '"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
        }
        if (!empty($sqlRow['movielink5'])) {
            print '<a href="' . $sqlRow['movielink5'] . '?iframe=true&width=100%&height=100%" rel="prettyPhoto[iframes]" title="' . $sqlRow['title'] . '"><img src="http://tipmypicks.com/cssmovie/images/watchmovie.png" class="watchmovie"></a>';
        }

    }
} 
else 
{
    print 'Error!'; 
}