473,386 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

shortening code

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
die(mysql_error());
$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?
Jun 2 '08 #1
3 1404
On Tue, 13 May 2008 02:10:57 +0200, vern <vw******@gmail.comwrote:
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
die(mysql_error());
$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?
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 files
could result in a much more troublesome update.

However there is offcourse a simpler way:

mysql_query("UPDATE {$db_db}.phpbb_forums SET forum_posts = forum_posts
+ $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 rwasmus.nl. [/SPAM]
Jun 2 '08 #2
vern escribió:
$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
die(mysql_error());
$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());
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.

--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Jun 2 '08 #3
Greetings, vern.
In reply to Your message dated Tuesday, May 13, 2008, 04:10:57,
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
die(mysql_error());
$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?
For me, I'd just trow away the database prefix and use mysql_select_db()
instead.
It's rare case when I need to work with more than one database in a single script.
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Jun 2 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Herb Kauhry | last post by:
Just got into PHP. Bought and read Core PHP Programming by Atkinson and Suraski. I have a question that didn't seem to be addressed there or anywhere else I've looked so far. So the answer is...
2
by: kaptain kernel | last post by:
how does one shorten an array based on a key value. Say I have this: array("Car1"=>"Porsche","Car2"=>"Merc","Car3"=>"Ford","Car4"=>"Toyota"); and I want to pop "Car2" out of the array,...
51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
4
by: Dariusz | last post by:
I have a guestbook on one of my sites where the entires are formatted to be x pixles in width. Now the text is input and output from a "blob" field from a database... but when people input long...
4
by: jason | last post by:
Hello. Newbie on SQL and suffering through this. I have two tables created as such: drop table table1; go drop table table2; go
6
by: kberry | last post by:
I am clearing Textboxes on a form... this is loop I have came up with but was wondering if it can be shorter or not as long... Can anyone help? Dim controlOnForm As Control 'Places a control...
2
by: 1051109210 | last post by:
if (seat=="X" && seat=="X" && seat=="X" && seat=="X" && seat=="X" && seat=="X" && seat=="X" && seat=="X" && seat=="X" && seat=="X" &&
5
by: hadge26 | last post by:
Hi Guys, Sorry if these are noob questions but i have a few issues with a website im trying to build. Ok its a review site, where the user can type their review into a submit page.. got all...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.