How to make it shorter and better?

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

    How to make it shorter and better?

    Hello,,,

    I don't know much about PHP+SQL but I have to find the better way to make my
    query work and to be shorter and better.
    All help will be appreciated.

    //Check if IP exists in banIP
    $res = mysql_query("SE LECT ip FROM banIP WHERE ip =
    '".$_SERVER['REMOTE_ADDR']."'");
    $res = mysql_num_rows( $res);
    if($res != 0)
    die;
    mysql_free_resu lt($res);

    //Fetch Time
    $timestamp = time();
    $timeout = $timestamp - 900;
    //Delete Users
    $del = mysql_query("DE LETE FROM caspionet WHERE timestamp<$time out");

    //Check if IP exists in caspionet
    $res = mysql_query("SE LECT ip FROM caspionet WHERE ip =
    '".$_SERVER['REMOTE_ADDR']."'");
    $res = mysql_num_rows( $res);
    if($res == 0){
    //Insert User
    $ins = mysql_query("IN SERT INTO caspionet (timestamp, ip)
    VALUES('$timest amp','".$_SERVE R['REMOTE_ADDR']."')");
    }
    mysql_free_resu lt($res);

    //Fetch Users Online
    $res = mysql_query("SE LECT DISTINCT ip FROM caspionet");
    while ($row = mysql_fetch_arr ay($res, MYSQL_BOTH)){
    print("IP:$row[0]");
    }
    mysql_free_resu lt($res);

    Thank you
    A.K.


  • Sandman

    #2
    Re: How to make it shorter and better?

    In article <e2v6ho$ht7$2@n ews.tiscali.fr> , "AlGorr" <a@a.com> wrote:
    [color=blue]
    > <snip>[/color]

    It's pretty tight as it is.


    # Setup
    $ip = $_SERVER["REMOTE_ADD R"];
    $timestamp = time()-900;

    # Should we die?
    $c = mysqlquery("sel ect count(id) from banIP where ip = '$ip'");
    list ($count) = mysql_fetch_ass oc($c);
    if ($count) die;

    # Remove old stuff
    mysql_query("de lete from caspionet where timestamp < $timestamp");

    # In Caspionet?
    $c = mysqlquery("sel ect count(id) from caspionet where ip = '$ip'");
    list ($count) = mysql_fetch_ass oc($c);
    if (!$count){
    mysql_query("in sert into caspionet...");
    }

    # Who's online?
    $ips = mysql_query("se lect distinct ip from caspionet");
    while ($r = mysql_fetch_ass oc($ips)){
    print "IP: $r[ip]\n";
    }






    --
    Sandman[.net]

    Comment

    • AlGorr

      #3
      Re: How to make it shorter and better?

      Sandman thanks a lot !!!!!!!!!!!!!!! !!!!
      That's much better! I will use your exemple.

      Only look:
      we have to ask 4 times the server for these 4 queries. It's a SQL question.
      Is it possible to put for ex. 2 queries in one big and make work the server
      more faster?

      Regards
      A.K.


      Comment

      • Jerry Stuckle

        #4
        Re: How to make it shorter and better?

        AlGorr wrote:[color=blue]
        > Hello,,,
        >
        > I don't know much about PHP+SQL but I have to find the better way to make my
        > query work and to be shorter and better.
        > All help will be appreciated.
        >
        > //Check if IP exists in banIP
        > $res = mysql_query("SE LECT ip FROM banIP WHERE ip =
        > '".$_SERVER['REMOTE_ADDR']."'");
        > $res = mysql_num_rows( $res);
        > if($res != 0)
        > die;
        > mysql_free_resu lt($res);
        >
        > //Fetch Time
        > $timestamp = time();
        > $timeout = $timestamp - 900;
        > //Delete Users
        > $del = mysql_query("DE LETE FROM caspionet WHERE timestamp<$time out");
        >
        > //Check if IP exists in caspionet
        > $res = mysql_query("SE LECT ip FROM caspionet WHERE ip =
        > '".$_SERVER['REMOTE_ADDR']."'");
        > $res = mysql_num_rows( $res);
        > if($res == 0){
        > //Insert User
        > $ins = mysql_query("IN SERT INTO caspionet (timestamp, ip)
        > VALUES('$timest amp','".$_SERVE R['REMOTE_ADDR']."')");
        > }
        > mysql_free_resu lt($res);
        >
        > //Fetch Users Online
        > $res = mysql_query("SE LECT DISTINCT ip FROM caspionet");
        > while ($row = mysql_fetch_arr ay($res, MYSQL_BOTH)){
        > print("IP:$row[0]");
        > }
        > mysql_free_resu lt($res);
        >
        > Thank you
        > A.K.
        >
        >[/color]

        Please cross-post - don't multipost!

        Answered in comp.databases. mysql.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        • Sandman

          #5
          Re: How to make it shorter and better?

          In article <e2vf80$cn7$1@n ews.tiscali.fr> , "AlGorr" <a@a.com> wrote:
          [color=blue]
          > Sandman thanks a lot !!!!!!!!!!!!!!! !!!!
          > That's much better! I will use your exemple.
          >
          > Only look:
          > we have to ask 4 times the server for these 4 queries. It's a SQL question.
          > Is it possible to put for ex. 2 queries in one big and make work the server
          > more faster?[/color]

          Nah, not more efficient. It's still four queries with four different
          rules. They all need to be executed separately.



          --
          Sandman[.net]

          Comment

          • Colin McKinnon

            #6
            Re: How to make it shorter and better?

            Sandman wrote:
            [color=blue]
            > In article <e2vf80$cn7$1@n ews.tiscali.fr> , "AlGorr" <a@a.com> wrote:
            >[color=green]
            >> Sandman thanks a lot !!!!!!!!!!!!!!! !!!!
            >> That's much better! I will use your exemple.
            >>
            >> Only look:
            >> we have to ask 4 times the server for these 4 queries. It's a SQL
            >> question. Is it possible to put for ex. 2 queries in one big and make
            >> work the server more faster?[/color]
            >
            > Nah, not more efficient. It's still four queries with four different
            > rules. They all need to be executed separately.
            >[/color]

            Actually you could do strange things with cartesian products but it may be
            messy depending on how the database is set up. Presumably if AlGorr needs
            to look at it it's because there is a performance issue. First place to
            start with performance issues in database queries is.....the database. Not
            the application language.

            C.

            Comment

            • Sandman

              #7
              Re: How to make it shorter and better?

              In article <aJ95g.39$A54.2 5@newsfe2-gui.ntli.net>,
              Colin McKinnon
              <colin.thisisno tmysurname@ntlw orld.deletemeun lessURaBot.com> wrote:
              [color=blue]
              > Sandman wrote:
              >[color=green]
              > > In article <e2vf80$cn7$1@n ews.tiscali.fr> , "AlGorr" <a@a.com> wrote:
              > >[color=darkred]
              > >> Sandman thanks a lot !!!!!!!!!!!!!!! !!!!
              > >> That's much better! I will use your exemple.
              > >>
              > >> Only look:
              > >> we have to ask 4 times the server for these 4 queries. It's a SQL
              > >> question. Is it possible to put for ex. 2 queries in one big and make
              > >> work the server more faster?[/color]
              > >
              > > Nah, not more efficient. It's still four queries with four different
              > > rules. They all need to be executed separately.
              > >[/color]
              >
              > Actually you could do strange things with cartesian products but it may be
              > messy depending on how the database is set up. Presumably if AlGorr needs
              > to look at it it's because there is a performance issue. First place to
              > start with performance issues in database queries is.....the database. Not
              > the application language.[/color]

              Indeed - I didn't interprete "shorter and better" as being that,
              though. BUt now that you say it...

              Indexes is a good place to start for single-value lookups

              --
              Sandman[.net]

              Comment

              Working...