Hello all,
I have attempted to write my own forum using PHP and MySQL. It works,
but the efficiency is far from ideal. It takes a long time to load e.g.
the list with threads in a certain forum.
I think the problem has to do with the fact that I have several SQL
calls in nested loops, so if a large thread list has to be loaded, there
are a lot of SQL queries to be executed. I have posted the code that
initialises the forum below, so you can see what I mean.
My question: is there a more efficient way to initialise my forum or is
it better to begin from scratch?
Here's the code:
function Forum($id) {
// initialize the threads-attribute
$this->threads = array();
// get the information for this forum from the database
$con = dbConnect();
if ($con) {
$sql = "SELECT * FROM forums WHERE forum_id = '$id'";
$query = mysql_query($sql);
$res = mysql_fetch_object($query);
$this->forum_id = $res->forum_id;
$this->name = $res->name;
$this->desc = $res->description;
// get the contents of the threads-attribute from the database
$sql = "SELECT * FROM threads WHERE forum_id = '$id'";
$query = mysql_query($sql);
while ($res = mysql_fetch_object($query)) {
$thread_id = $res->thread_id;
$subject = $res->subject;
// get the messages for this thread
$messages = array();
$sql2 = "SELECT * FROM messages WHERE thread_id =
'$res->thread_id'";
$query2 = mysql_query($sql2);
while ($res2 = mysql_fetch_object($query2)) {
$msg_id = $res2->msg_id;
$author_id = $res2->author_id;
$thread_id = $res2->thread_id;
$post_date = $res2->post_date;
$contents = $res2->contents;
$msg = new Message($msg_id, $author_id, $thread_id, $post_date,
$contents);
$messages[] = $msg;
}
$thread = new Thread($thread_id, $subject, $messages);
$this->threads[] = $thread;
}
}
}
--
Daan Stolp