1

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.

4 Answers 4

3

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']; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

...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.
3

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.

2 Comments

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

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

1 Comment

In my database it will eventually be possible to have a haveplayed = "1"
0

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>";   
                }

3 Comments

But I've set the default value actually as '0'.
I've tried what you said, and it's still giving me a 500 error (out of memory)
In my database it will eventually be possible to have a haveplayed = "1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.