shortening code

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Just curious, how would I shorten the following code?

$sql_forum_posts_num = "SELECT forum_posts FROM ".
$db_db.".phpbb_forums WHERE forum_id = '$forum_id'";
$forum_posts_num = mysql_query($sql_forum_posts_num) or
$forum_posts_num_real = mysql_fetch_row($forum_posts_num);
$forum_posts_real_num = $forum_posts_num_real[0] + $num_topics_real;
$forum_topics_num_update_sql = "UPDATE ".$db_db.".phpbb_forums SET
forum_posts = '$forum_posts_real_num' WHERE forum_id = '$forum_id'";
mysql_query($forum_topics_num_update_sql) or die(mysql_error());

6 lines for something so simple seems so much. Is there any syntax I
can change to cut this down?

Re: shortening code

Quoted text here. Click to load it

Shorter code isn't always better. If it works I'd leave it, escpecially =

since it seems to be from the phpBB packages, and manually altering file=
s  =

could result in a much more troublesome update.

However there is offcourse a simpler way:

mysql_query("UPDATE .phpbb_forums SET forum_posts = forum_post=
s  =

+ $num_topics_real WHERE forum_id = $forum_id");

Keep SQL injection in mind...
-- =

Rik Wasmus
[SPAM] Now temporarily looking for some smaller PHP/MySQL projects/work =
to  =

fund a self developed bigger project, mail me at rik at [/SP=

Re: shortening code

vern escribió:
Quoted text here. Click to load it

This code seems to put a lot of effort in creating unique names for
variables that do not seem of any use outside this block. I'd say that
replacing stuff like $sql_forum_posts_num and
$forum_topics_num_update_sql with $sql would be a sort of improvement.
Also, some blank lines would make it look less like a brick.

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor al baño María:

Re: shortening code

Greetings, vern.
In reply to Your message dated Tuesday, May 13, 2008, 04:10:57,

Quoted text here. Click to load it

For me, I'd just trow away the database prefix and use mysql_select_db()
It's rare case when I need to work with more than one database in a single


Site Timeline