0

There is fan based php game, and one of its file's has over 300 queries. I thought of optimizing it and making it with less queries so it doent crash, and so it runs faster.

So my question is how do i make a php script with less queries.

Example:

if ('this' == 'this')
{
   mysql_query("INSERT INTO users VALUES(' ', '$user', 'pass'"); 
}


if ('1' == '1')
{
   mysql_query("INSERT INTO users VALUES(' ', '$user', 'pass'"); 
}

I thought of trying this:

function do()
{
    mysql_query("INSERT INTO users VALUES(' ', '$user', 'pass'");     
}


if ('crap' == 'crap')
{
    do();    
}


if ('1' == '1')
{     
    do();    
}

But then realized it the same, so any ideas ? thanks

jh314
  • 27,144
  • 16
  • 62
  • 82
user2578273
  • 111
  • 1
  • 6

1 Answers1

0

In your example all you need to do is have the variables change on the if statements and run one query.

function do(insert your parameters)
{

  if (blah == blah)
  {


      if ('1' == '1')
      {     
        // set variables 
      }

      elseif('crap' == 'crap') 
      {
        //set variables
      }

      else 
      {
      // set variables
      }

      mysql_query("INSERT INTO users VALUES(' ', '$user', 'pass'");     

  }

  else {

  return false;

  }

}

This may also be a great canidate for the switch case statement. http://php.net/manual/en/control-structures.switch.php

switch ($myvariable) {
    case "crap":
        $myvariable = "blah";
        break;
    case 1:
        $myvariable = "blah";
        break;
    default:
       $myvariable = "blah";
}

That is a quick example but not knowing exactly what you need this may help. I also suggest you use mysqli instead of mysql as mysql has been deprecated.

Chris78
  • 422
  • 2
  • 5
  • 15
  • I know good php :), i was just asking for a way to use less queries so the server running the script would not crash. – user2578273 Jul 13 '13 at 01:57
  • Oh ok :) We'll maybe I gave you a few good ideas. As I mentioned I really don't know your script and how things are calling the DB but maybe joining tables will help cut down some of the queries as well. – Chris78 Jul 13 '13 at 02:04