views:

63

answers:

4

Hey all, 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.

+3  A: 

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.

Iacopo
So I just replace 'while' with 'foreach' and keep my existing code. The default value of 'haveplayed' actually is 0.
Sam
In my database it will eventually be possible to have a haveplayed = "1"
Sam
I see, thankyou :).
Sam
A: 

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>";   
                }
ILMV
But I've set the default value actually as '0'.
Sam
I've tried what you said, and it's still giving me a 500 error (out of memory)
Sam
In my database it will eventually be possible to have a haveplayed = "1"
Sam
+1  A: 

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)?

contagious
In my database it will eventually be possible to have a haveplayed = "1"
Sam
Ohhh I see, thankyou.
Sam
+2  A: 

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']; 
}
Max
...I am such an idiot. Thankyou :). Another failing of my ability to recognise my own codes faults beyond simple syntax errors.My code is sometimes redundant like that...Thanks heaps.
Sam