简体   繁体   中英

MySQL & PHP while code is out of memory, when there is only one row

why is this code throwing an out of memory error, when there is only 1 row in the database..

$request_db = mysql_query("SELECT * FROM requests
                WHERE haveplayed='0'") or die(mysql_error());  
                $request = mysql_fetch_array( $request_db );
                echo "<table border=\"1\" align=\"center\">";
                while ( $request['haveplayed'] == "0" ) {
                    echo "<tr><td>";
                    echo $request['SongName'];  
                    echo "</td><td>";
                    echo "<tr><td>";
                    echo $request['Artist'];    
                    echo "</td><td>";
                    echo "<tr><td>";
                    echo $request['DedicatedTo'];
                    echo "</td><td>";   
                }
                echo "</table>";

Thanks.

Because in PHP

null == 0 == '0'

So it is looping

Use the '===' operator or better yet 'isset()'

while ( isset($request['haveplayed']) && $request['haveplayed'] == '0')

Furthermore, the use of while is quite useless in this code: maybe you want to fetch a new array inside the loop.

Hm, to be honest, I dont understand the use of while in this case. You are only fetching one row with this code (even if there is more then one row in the DB!). Maybe you tried something like if($request['haveplayed'] == 0) but that wouldnt make too much sense, also, as the query only returns rows with haveplayed equal 0 .

As far as I can tell, you intend to output one or more rows from the requests table where haveplayed equals 0 . Wouldnt it be more like this then?

$request_db = mysql_query("SELECT * FROM requests WHERE haveplayed='0'")
              or die(mysql_error());

while($request = mysql_fetch_array( $request_db )) {
    // output stuff here ...
    echo $request['SongName']; 
}

that's because you have an infinite loop there. your while statement is always true because that's the only thing you're pulling from the database: haveplayed is always '0', so it will never stop because that value is never changed. Bascially, you're while loop is no needed at all, as the only things you are pulling from the database are exactly what you're checking in the while conditional.

maybe do a foreach ($request as $r) ?

Without putting too much effort in, what datatype is haveplayed? because it looks like your while loop is never being satisfied, try this instead:

$request_db = mysql_query("SELECT * FROM requests
                WHERE haveplayed='0'") or die(mysql_error());  
                $request = mysql_fetch_array( $request_db );
                echo "<table border=\"1\" align=\"center\">";
                while ( $request['haveplayed'] == 0 ) {
// or           while ( $request['haveplayed'] == false ) {
                    echo "<tr><td>";
                    echo $request['SongName'];  
                    echo "</td><td>";
                    echo "<tr><td>";
                    echo $request['Artist'];    
                    echo "</td><td>";
                    echo "<tr><td>";
                    echo $request['DedicatedTo'];
                    echo "</td><td>";   
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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