简体   繁体   English

如何在PHP中巧妙地停止“ while循环”?

[英]How to cleverly stop “while loop” in PHP?

I'm having trouble with creating code that echoes a bunch of stuff that is corresponding to the mysql database row. 我在创建代码时遇到麻烦,该代码回显了对应于mysql数据库行的一堆东西。 It needs to keep creating the content until all rows are used and then stop. 它需要继续创建内容,直到使用完所有行,然后停止。

The amount of rows currently present is 5 当前存在的行数为5

But for some reason the php file causes the browser to keep loading (it never ends). 但是由于某种原因,php文件会导致浏览器继续加载(永远不会结束)。 Any help would be appreciated! 任何帮助,将不胜感激! Thanks! 谢谢!

The problem was not in this PHP file but in my HTML code, thanks everybody for helping 问题不在此PHP文件中,而在我的HTML代码中,谢谢大家的帮助

<?php 

mysql_connect ("localhost", "root", "") or die ("We couldn't connect!");
mysql_select_db ("dr");
mysql_query ("SELECT * FROM songs");
$result = mysql_query ("SELECT * FROM songs");

while ($row=mysql_fetch_array($result)) {

    $name = $row ['songname'];
    $genres = $row ['songgenres'];
    $mediafire = $row ['mediafirelink'];
    $dropbox = $row ['dropboxlink'];
    $source = $row ['audiosource'];

    echo "
    <div class='playing'>
            <!-- ======== Song Name ======== -->
                <li class='songnameli' id='$source'>
                    <span class='info'>$name</span>
                    <audio>
                        <source src='music/singles/$source.mp3'>
                        <source src='music/singles/$source.ogg'>
                    </audio>
                </li>

            <!-- ======== Playlist ======== -->
                <li class='playlistli'>
                    <img src='icons/addtoplaylist.png' title='Add tot the playlist!' />
                </li>

            <!-- ======== Genres ======== -->
                <li class='genresli'>
                    <img src='icons/genres.png' title='Related genres' />
                    <span class='addedtext genres'>$genres</span>
                </li>

            <!-- ======== Social Media links ======== -->
                <li>
                    <span>
                    <img src='icons/share.png' alt='Share this with your friends!' title='Share this!'>
                    <!-- /// facebook /// -->
                        <a href='http://www.facebook.com/sharer.php?u=http://www.declassified-recordings.com'
                           class='addedtext nlink'
                           target='blank_'
                           onclick='popup (this.href, 800, 500); return false'>Facebook
                        </a>

                    <span>/</span>

                    <!-- /// Twitter /// -->
                        <a href='http://twitter.com/share?
                            text=Thank%20you%20For%20Sharing!%20It%20means%20the%20world%20to%20us!%40Declassifi3d%20
                            &url=http://www.declassified-recordings.com'
                           class='twitterlink nlink'
                           target='blank_'
                           onclick='popup (this.href, 800, 500); return false'>Twitter</a>


                    </span>

                </li>

            <!-- ======== Download links ======== -->

                <li>
                    <img src='icons/download.png' title='Download!'  />
                    <span>
                        <!-- /// Mediafire /// -->
                            <a href='$mediafire'
                               class='addedtext nlink'
                               target='_blank'>Mediafire</a>

                        <span class='genres'>/</span>

                        <!-- /// Dropbox /// -->
                            <a href='$mediafire'
                               class='twitterlink nlink'
                               target='_blank'>Dropbox</a>


                    </span>
                </li>
    </div>";
}
mysql_close ();
?>

First, why do you have these two mysql_query commands right on top of each other like this: 首先,为什么您要像下面这样将这两个mysql_query命令彼此相对:

mysql_query ("SELECT * FROM songs");
$result = mysql_query ("SELECT * FROM songs");

Just get rid of that first one & maybe set the actual SQL into variable like this: 只要摆脱掉第一个,就可以将实际的SQL设置成这样的变量:

$query_sql = "SELECT * FROM songs";
$result = mysql_query ($query_sql);

But specific to the question, change your MySQL query from this: 但是针对该问题,请从此更改您的MySQL查询:

"SELECT * FROM songs"

To use LIMIT as part of your SELECT : 要将LIMIT用作SELECT一部分:

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT子句可用于约束SELECT语句返回的行数。 LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements). LIMIT接受一个或两个数字参数,这两个参数都必须是非负整数常量(使用预处理语句时除外)。

For example, like this: 例如,像这样:

"SELECT * FROM songs LIMIT 0,50"

Then you can code some logic to handle pagination such as this: 然后,您可以编写一些逻辑来处理分页,例如:

$start = 0;
$how_many = 50;
$query_sql = "SELECT * FROM songs LIMIT $start, $how_many";
$result = mysql_query ($query_sql);

My first question would be how big is the table? 我的第一个问题是桌子有多大?

If it is a couple thousand rows, I recommend making it paged and only load 50 songs at a time. 如果是几千行,建议将其分页,一次仅加载50首歌曲。

If it is only a couple hundred, then it is just taking the server a while to process the data. 如果只有几百个,则只需要服务器花费一些时间来处理数据。

Also, to not correct your code, but possibly help out, try this: 另外,要不更正您的代码,但可能会有所帮助,请尝试以下操作:

<?
    $connection = mysqli_connect ("localhost", "root", "", "dr") or die ("We couldn't connect!");
    $result = mysqli_query($connection, "SELECT * FROM songs");
    while ($row=mysqli_fetch_array($result)) {

        $name = $row ['songname'];
        $genres = $row ['songgenres'];
        $mediafire = $row ['mediafirelink'];
        $dropbox = $row ['dropboxlink'];
        $source = $row ['audiosource'];

        echo "
        <div class='playing'>
                <!-- ======== Song Name ======== -->
                    <li class='songnameli' id='$source'>
                        <span class='info'>$name</span>
                        <audio>
                            <source src='music/singles/$source.mp3'>
                            <source src='music/singles/$source.ogg'>
                        </audio>
                    </li>

                <!-- ======== Playlist ======== -->
                    <li class='playlistli'>
                        <img src='icons/addtoplaylist.png' title='Add tot the playlist!' />
                    </li>

                <!-- ======== Genres ======== -->
                    <li class='genresli'>
                        <img src='icons/genres.png' title='Related genres' />
                        <span class='addedtext genres'>$genres</span>
                    </li>

                <!-- ======== Social Media links ======== -->
                    <li>
                        <span>
                        <img src='icons/share.png' alt='Share this with your friends!' title='Share this!'>
                        <!-- /// facebook /// -->
                            <a href='http://www.facebook.com/sharer.php?u=http://www.declassified-recordings.com'
                               class='addedtext nlink'
                               target='blank_'
                               onclick='popup (this.href, 800, 500); return false'>Facebook
                            </a>

                        <span>/</span>

                        <!-- /// Twitter /// -->
                            <a href='http://twitter.com/share?
                                text=Thank%20you%20For%20Sharing!%20It%20means%20the%20world%20to%20us!%40Declassifi3d%20
                                &url=http://www.declassified-recordings.com'
                               class='twitterlink nlink'
                               target='blank_'
                               onclick='popup (this.href, 800, 500); return false'>Twitter</a>


                        </span>

                    </li>

                <!-- ======== Download links ======== -->

                    <li>
                        <img src='icons/download.png' title='Download!'  />
                        <span>
                            <!-- /// Mediafire /// -->
                                <a href='$mediafire'
                                   class='addedtext nlink'
                                   target='_blank'>Mediafire</a>

                            <span class='genres'>/</span>

                            <!-- /// Dropbox /// -->
                                <a href='$mediafire'
                                   class='twitterlink nlink'
                                   target='_blank'>Dropbox</a>


                        </span>
                    </li>
        </div>";


    }




    mysqli_close($connection);

?>

I would move away from mysql_connection and the old format, it is outdated, I would use mysqli for everything. 我将不再使用mysql_connection和旧格式,它已经过时了,我将使用mysqli进行所有操作。 http://www.php.net//manual/en/book.mysqli.php http://www.php.net//manual/en/book.mysqli.php

If i were you i would change 2 things, one is the limit of results like @JakeGould suggested and i would change the way you are echoing the HTML code (this will make it easier for the interpeter) like so: 如果我是我,我将更改2件事,其中之一是@JakeGould建议之类的结果限制,并且我将更改您回显HTML代码的方式(这将使插入者更容易),如下所示:

also u forgot to put your <li> inside a <ul> 您也忘记将您的<li>放在<ul>

<?php


while ($row=mysql_fetch_array($result)) {

$name = $row ['songname'];
$genres = $row ['songgenres'];
$mediafire = $row ['mediafirelink'];
$dropbox = $row ['dropboxlink'];
$source = $row ['audiosource'];

?>

<div class='playing'>
    <ul>>
        <!-- ======== Song Name ======== -->
            <li class='songnameli' id='$source'>
                <span class='info'><?php echo $name; ?></span>
                <audio>
                    <source src='music/singles/$source.mp3'>
                    <source src='music/singles/$source.ogg'>
                </audio>
            </li>

        <!-- ======== Playlist ======== -->
            <li class='playlistli'>
                <img src='icons/addtoplaylist.png' title='Add tot the playlist!' />
            </li>

        <!-- ======== Genres ======== -->
            <li class='genresli'>
                <img src='icons/genres.png' title='Related genres' />
                <span class='addedtext genres'><?php echo $genres; ?></span>
            </li>

        <!-- ======== Social Media links ======== -->
            <li>
                <span>
                <img src='icons/share.png' alt='Share this with your friends!' title='Share this!'>
                <!-- /// facebook /// -->
                    <a href='http://www.facebook.com/sharer.php?u=http://www.declassified-recordings.com'
                       class='addedtext nlink'
                       target='blank_'
                       onclick='popup (this.href, 800, 500); return false'>Facebook
                    </a>

                <span>/</span>

                <!-- /// Twitter /// -->
                    <a href='http://twitter.com/share?
                        text=Thank%20you%20For%20Sharing!%20It%20means%20the%20world%20to%20us!%40Declassifi3d%20
                        &url=http://www.declassified-recordings.com'
                       class='twitterlink nlink'
                       target='blank_'
                       onclick='popup (this.href, 800, 500); return false'>Twitter</a>


                </span>

            </li>

        <!-- ======== Download links ======== -->

            <li>
                <img src='icons/download.png' title='Download!'  />
                <span>
                    <!-- /// Mediafire /// -->
                        <a href='<?php echo $mediafire; ?>'
                           class='addedtext nlink'
                           target='_blank'>Mediafire</a>

                    <span class='genres'>/</span>

                    <!-- /// Dropbox /// -->
                        <a href='<?php echo $mediafire; ?>'
                           class='twitterlink nlink'
                           target='_blank'>Dropbox</a>


                </span>
            </li>
        </ul>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM