shortening code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vern

    shortening code

    Just curious, how would I shorten the following code?

    $sql_forum_post s_num = "SELECT forum_posts FROM ".
    $db_db.".phpbb_ forums WHERE forum_id = '$forum_id'";
    $forum_posts_nu m = mysql_query($sq l_forum_posts_n um) or
    die(mysql_error ());
    $forum_posts_nu m_real = mysql_fetch_row ($forum_posts_n um);
    $forum_posts_re al_num = $forum_posts_nu m_real[0] + $num_topics_rea l;
    $forum_topics_n um_update_sql = "UPDATE ".$db_db.".phpb b_forums SET
    forum_posts = '$forum_posts_r eal_num' WHERE forum_id = '$forum_id'";
    mysql_query($fo rum_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?
  • Rik Wasmus

    #2
    Re: shortening code

    On Tue, 13 May 2008 02:10:57 +0200, vern <vwenberg@gmail .comwrote:
    Just curious, how would I shorten the following code?
    >
    $sql_forum_post s_num = "SELECT forum_posts FROM ".
    $db_db.".phpbb_ forums WHERE forum_id = '$forum_id'";
    $forum_posts_nu m = mysql_query($sq l_forum_posts_n um) or
    die(mysql_error ());
    $forum_posts_nu m_real = mysql_fetch_row ($forum_posts_n um);
    $forum_posts_re al_num = $forum_posts_nu m_real[0] + $num_topics_rea l;
    $forum_topics_n um_update_sql = "UPDATE ".$db_db.".phpb b_forums SET
    forum_posts = '$forum_posts_r eal_num' WHERE forum_id = '$forum_id'";
    mysql_query($fo rum_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("UP DATE {$db_db}.phpbb_ forums SET forum_posts = forum_posts
    + $num_topics_rea l 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]

    Comment

    • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

      #3
      Re: shortening code

      vern escribió:
      $sql_forum_post s_num = "SELECT forum_posts FROM ".
      $db_db.".phpbb_ forums WHERE forum_id = '$forum_id'";
      $forum_posts_nu m = mysql_query($sq l_forum_posts_n um) or
      die(mysql_error ());
      $forum_posts_nu m_real = mysql_fetch_row ($forum_posts_n um);
      $forum_posts_re al_num = $forum_posts_nu m_real[0] + $num_topics_rea l;
      $forum_topics_n um_update_sql = "UPDATE ".$db_db.".phpb b_forums SET
      forum_posts = '$forum_posts_r eal_num' WHERE forum_id = '$forum_id'";
      mysql_query($fo rum_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_post s_num and
      $forum_topics_n um_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
      --

      Comment

      • AnrDaemon

        #4
        Re: shortening code

        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_post s_num = "SELECT forum_posts FROM ".
        $db_db.".phpbb_ forums WHERE forum_id = '$forum_id'";
        $forum_posts_nu m = mysql_query($sq l_forum_posts_n um) or
        die(mysql_error ());
        $forum_posts_nu m_real = mysql_fetch_row ($forum_posts_n um);
        $forum_posts_re al_num = $forum_posts_nu m_real[0] + $num_topics_rea l;
        $forum_topics_n um_update_sql = "UPDATE ".$db_db.".phpb b_forums SET
        forum_posts = '$forum_posts_r eal_num' WHERE forum_id = '$forum_id'";
        mysql_query($fo rum_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 <anrdaemon@free mail.ru>

        Comment

        Working...