tags:

views:

141

answers:

4

What is causing my php code to freeze? I know it's cause of the while loop, but I have $max_threads--; at the end so it shouldn't do that.

<html>
    <head>
        <?php
            $db = mysql_connect("host","name","pass") or die("Can't connect to host");
            mysql_select_db("dbname",$db) or die("Can't connect to DB"); 

            $sql_result = mysql_query("SELECT MAX(Thread) FROM test_posts", $db);

            $rs = mysql_fetch_row($sql_result);

            $max_threads = $rs[0];

            $board = $_GET['board'];
        ?>



    </head>

    <body>


        <?php


            While($max_threads >= 0)
            {
                $sql_result = mysql_query("SELECT MIN(ID) FROM test_posts WHERE Thread=".$max_threads."", $db);
                $rs = mysql_fetch_row($sql_result);

                $sql_result = mysql_query("SELECT post FROM test_posts WHERE ID=".$rs[0]."", $db);
                $post = mysql_fetch_row($sql_result);

                $sql_result = mysql_query("SELECT name FROM test_posts WHERE ID=".$rs[0]."", $db);
                $name = mysql_fetch_row($sql_result);

                $sql_result = mysql_query("SELECT trip FROM test_posts WHERE ID=".$rs[0]."", $db);
                $trip = mysql_fetch_row($sql_result);

                if(!empty($post))
                    echo'<div class="postbox"><h4>'.$name[0].'['.$trip[0].']</h4><hr />' . $post[0] . '<br /><hr />[<a href="http://prime.programming-designs.com/test_forum/viewthread.php?thread='.$max_threads.'"&gt;Reply&lt;/a&gt;]&lt;/div&gt;';

                $max_threads--;
            }

        ?>
    </body>
</html>
+1  A: 

I'm thinking it's because you're hitting the sql database 4 times per loop. Is there any way you can maybe access it all at once, and then parse the incoming data from there?

$dbsql = 'SELECT * FROM my_database';
$result = mysql_query($dbsql);

while($row = mysql_fetch_array($result)) {
     // Parse information here, rather than 
     // accessing the database for individual variables... 
}

Something like that.

Update:

Other than what I've already said (and you've dismissed) all I see are some here & there coding quirks:

This part didn't have a space between echo and the string. The 'hr' element didn't have a starting bracket.

echo '<div class="postbox"><h4>'.$name[0].'['.$trip[0].']</h4><hr>' . $post[0] . '<br /><hr />[<a href="http://prime.programming-designs.com/test_forum/viewthread.php?thread='.$max_threads.'"&gt;Reply&lt;/a&gt;]&lt;/div&gt;';

'while' Shouldn't be capitalized.

while($max_threads >= 0)

Again, clean code is a good place to start, but that's all I've got, personally. I just recently cleaned up my own site, which was crashing IE (and no other browser), simply because it had too many markup errors. Hope it helps.

dclowd9901
Maybe that'll be a problem later, but right now it's a total freeze with only two threads.
William
@William: Really, it's a problem now, even if it's not the cause of the freeze. Issuing 4 queries when you only need one is overkill, and makes the code less readable.
DisgruntledGoat
No, it's not a problem right now because it doesn't matter if the damn thing is freezing and crashing browsers before loading the page, duh.
William
@William: When you say "freeze", does it literally crash the browser, or does the PHP just not fully execute?
dclowd9901
It would crash the broswer. x_x
William
So what did it turn out being exactly? I'm very curious.
dclowd9901
A: 

Maybe you can scatter calls to this simple function in yer code:

function of($required)
{
    $args = func_get_args();
    var_dump($args);
    ob_flush();
    flush();
}
of(__LINE__, $max_threads);

You can also use something like this for your queries:

function mydb_query($query, $db = null)
{
    $args = func_get_args();
    $result = call_user_func_array('mysql_query', $args);
    if (!$result) {
        of(array(__FUNCTION__), mysql_error(), $sql);
        //return something else?
    }
    return $result;
}
$result = mydb_query("SELECT post, name, trip FROM test_posts WHERE ID = (SELECT MIN(ID) FROM test_posts WHERE Thread={$max_threads})", $db);

You should use mysqli/PDO/framework with support for prepared statements.

OIS
+2  A: 

First, I'd suggest completely getting rid of the extraneous HTML bits. Then, build up your code slowly, line-by-line to see if you can find the offending line. So write a script that just connects to the database and see what happens.

If you find for example that this code...

<?php
    $db = mysql_connect("host","name","pass") or die("Can't connect to host");
    mysql_select_db("dbname",$db) or die("Can't connect to DB"); 
?>

...is causing the freeze on its own, then it could easily be a problem with the MySQL server.

However, if the browser itself is crashing, that sounds like an issue with your system rather than something that PHP or MySQL is doing...

DisgruntledGoat
+1  A: 

Try this 1 SQL query instead of those 1 + (4 * n) queries:

SELECT MIN(ID), post, name, trip FROM test_posts GROUP BY Thread

Maybe a LIMIT 50 (or whatever max # of threads to return) at the end as well, that could be a lot of data.

You can just loop over the results of this query instead of $max_threads and all the extra db calls, via while ($row = mysql_fetch_row($sql_result)) { /* echo(...); */ }.

Not sure that's exactly the same as what you're trying to fetch without knowing more about the data (getting the root post of each thread in a forum?), but it should be pretty close.

(P.S.: if that's a threaded 2ch-style forum deal, I'm not sure that's an ideal db design. A parent-child adjacency list might be better than maintaining a number count for each thread. Just a guess though.)

tadamson