+ Reply to Thread
Results 1 to 5 of 5

Thread: Help simplifying a php script to lower server resources

  1. #1
    brunoais is offline x10 Sophmore brunoais is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    111

    Help simplifying a php script to lower server resources

    Ok I have this php text:
    There is already coding that start the db connection.
    There are already some variables defined

    Ok This code is completely functional but it uses a lot the mysql_query().
    I'd like to ask for your assistance to help me reduce the server load without lowering the functionality
    If I could reduce to one single query it would be wonderful!
    (I accept any suggestion that works!)

    PHP Code:
    // create a new cURL resource
    $ch = curl_init();

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, SERVER_URL."/map.sql");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 


    $result= curl_exec($ch);
    $ally=explode(';
    ', $result);


    // close cURL resource, and free up system resources and accelerate the process
    curl_close($ch);

    ?>
    <INPUT TYPE="image" SRC="img/ok.gif">
    <br><br>
    <?=text('dba_3')?>
    <?
    //apaga a tabela para adicionar os dados
    mysql_query("TRUNCATE x_world") OR die(mysql_error());

    foreach( 
    $ally as $ally ) {

    $allyy=explode("'"$ally);

    $n=count($allyy);

    $aid_1=str_replace(",","",$allyy[$n-3]);

    $aid =explode(';',Wings);

    $i 0;
    do {
    if (
    $aid[$i]==$aid_1)
    {
    //echo $ally;
    mysql_query($ally)OR die(mysql_error());
    }
    $i++;
    } while (
    $i <((count($aid))));
    }
    ?>
    <INPUT TYPE="image" SRC="img/ok.gif">
    <br><br>
    <?=text('dba_4')?>...
    <?
    $sql
    ='update '.PREFIX.'dorf v, x_world x
    SET v.size = x.population, v.name = x.village
    where v.x = x.x and v.y = x.y'
    ;
    mysql_query($sql)OR die(mysql_error());
    ?>
    <INPUT TYPE="image" SRC="img/ok.gif">
    <br><br>
    <?=text('dba_5')?>...
    <?
    $sql
    ='truncate table '.PREFIX.'xtmp_uid';
    mysql_query($sql)OR die("<br> ".mysql_error());

    $sql='insert into '.PREFIX.'xtmp_uid
    select distinct uid, player, tid, aid, alliance
    from x_world'
    ;
    mysql_query($sql)OR die("<br> ".mysql_error());

    $sql='truncate table '.PREFIX.'xtmp_aid';
    mysql_query($sql)OR die("<br> ".mysql_error());

    $sql='insert into '.PREFIX.'xtmp_aid
    select distinct aid, alliance
    from x_world'
    ;
    mysql_query($sql)OR die("<br> ".mysql_error());
    ?>
    <INPUT TYPE="image" SRC="img/ok.gif">
    <br><br>
    <?=text('dba_6')?>...
    <?
    $sql
    ='update '.PREFIX.'xallys a, '.PREFIX.'xtmp_aid t
    SET a.alliance = t.alliance
    where a.aid = t.aid'
    ;
    mysql_query($sql)OR die("<br> ".mysql_error());
    ?>
    <?
    $sql
    ='UPDATE '.PREFIX.'spieler s LEFT OUTER JOIN '.PREFIX.'xtmp_uid u ON s.uid = u.uid
    SET s.aid = u.aid'
    ;
    mysql_query($sql)OR die("<br> ".mysql_error());
    ?>
    <INPUT TYPE="image" SRC="img/ok.gif">
    <br><br>
    <?=text('dba_7')?>

    </div>

    </body>
    </html>

  2. #2
    quantum1's Avatar
    quantum1 is offline x10Hosting Member quantum1 is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    near Nashville, TN
    Posts
    68

    Re: Help simplifying a php script to lower server resources

    Well....I don't think you can combine the above into one query since you are using update statements on different tables. The server load issue is unclear since you are looping using a variable called $ally and then executing sql based on $ally. Since the value(s) of $ally are not shown above it is hard to comment on that part of your source.
    Two rules of development:
    1) Computers work for people; People do not work for computers
    2) Maintainability is all that matters.

  3. #3
    brunoais is offline x10 Sophmore brunoais is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    111

    Re: Help simplifying a php script to lower server resources

    The variable $ally is only defined where you can spot it, no where else.
    Wings is an array separated by ";"
    Edit:
    PHP Code:
    $ally=explode(';
    '
    $result); 
    this is the value of $ally

    to know the value of $result go to
    http://s1.travian.pt/map.sql
    or
    http://s2.travian.pt/map.sql
    or
    http://s3.travian.pt/map.sql
    or
    http://s4.travian.pt/map.sql
    or
    http://s5.travian.pt/map.sql
    or
    http://s6.travian.pt/map.sql
    or
    http://speed.travian.pt/map.sql
    . They're all more or less the same size.
    Last edited by brunoais; 01-05-2009 at 01:33 PM. Reason: Automerged Doublepost

  4. #4
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: Help simplifying a php script to lower server resources

    you can use multiple tables in one query by using joins, but you cannot combine a truncate and insert into one command. each of those must have their own query.

    possible join examples are:

    PHP Code:
    mysql_query("SELECT u.username, p.signature, p.avatar FROM username AS u LEFT JOIN profile AS p ON u.uid=p.uid WHERE u.uid='".mysql_escape($_GET['id'])."' LIMIT 1"); 
    you can also use inner join, right join, as well as left join above. they all work the same way, it's just the data they return differ.

    http://www.w3schools.com/sql/sql_join.asp
    http://answers.yahoo.com/question/in...7082608AA7DwSh

  5. #5
    brunoais is offline x10 Sophmore brunoais is an unknown quantity at this point
    Join Date
    May 2008
    Posts
    111

    Re: Help simplifying a php script to lower server resources

    Yet for insert it must be a query for each line?

+ Reply to Thread

Similar Threads

  1. tons of PHP Resources
    By Chris S in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 01-16-2009, 10:07 AM
  2. Replies: 3
    Last Post: 03-10-2008, 12:22 PM
  3. How to protect images without htaccess using PHP
    By frznmnky in forum Tutorials
    Replies: 0
    Last Post: 12-26-2007, 11:51 AM
  4. Links to Many Many Resources!
    By Superpoo in forum Scripts & 3rd Party Apps
    Replies: 18
    Last Post: 03-29-2005, 06:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers