0

I have a little problem with getting the quickest way to work my PHP server (in making a game).

That's my function in PHP:

function getSomeInfoByDataId($id) {
    $arr = mysql_fetch_array(mysql_query("SELECT * FROM maps WHERE id='".$id."' LIMIT 1;"));
    if(!empty($arr)) {
        return $arr;
    }
 }
//$user - is array with all details from loged-in user

Question: What is the best and quickest way to speed up PHP server?

Way 1.

$myArray = array(
    'map_id' => getSomeInfoByDataId($user['map'])['id'],
    'map_name' => getSomeInfoByDataId($user['map'])['name'],
    'map_pvp' => getSomeInfoByDataId($user['map'])['pvp'],
    'map_img' => getSomeInfoByDataId($user['map'])['img']
);

Way 2.

$mapInfo = getSomeInfoByDataId($user['map']);
$myArray = array(
    'map_id' => $mapInfo['id'],
    'map_name' => $mapInfo['name'],
    'map_pvp' => $mapInfo['pvp'],
    'map_img' => $mapInfo['img']
);

Sorry if the question is a little bit crazy, but is very important for me.

Mr Pro Pop
  • 666
  • 5
  • 19
Xaller
  • 1
  • 2
  • 2
    How slow is the code currently? Have you benchmarked each method? You shouldn't use `mysql_*` functions. – chris85 Sep 24 '17 at 16:55
  • https://stackoverflow.com/q/60174/5905665 – Fabian N. Sep 24 '17 at 16:55
  • 1
    The fewer requests you can make against the database the better, because database access is an overhead.... option 1 makes 4 separate database requests, option 2 makes a single request.... though you should have been able to measure the difference yourself – Mark Baker Sep 24 '17 at 16:57
  • 6
    And DON'T use the mysql extension, use mysqli or PDO, because the old mysql extension has been completely dropped by PHP, so won't be available if you upgrade (and should be giving you deprecated warnings anyway, but I guess you're suppressing those); and it allows you to use prepared statements with bind variables – Mark Baker Sep 24 '17 at 17:00
  • It doesn't look like "Way 1" and "Way 2" are doing the same. – Paul Spiegel Sep 24 '17 at 17:11
  • @PaulSpiegel - They're generating the same result `$myArray`, but one is doing it with 4 database calls, the other with just 1 database call – Mark Baker Sep 24 '17 at 17:12
  • My bad.. I've overseen something. However.. I wouldn't use either way. I don't see a reason to recreate the array. – Paul Spiegel Sep 24 '17 at 17:20
  • @PaulSpiegel - agreed, selecting only the required columns and using aliases to get the `map_` prefix would be clearer code, thugh no significant performance difference from 2 – Mark Baker Sep 24 '17 at 17:23
  • @MarkBaker Yes.. for me it's more about unnecessary code. – Paul Spiegel Sep 24 '17 at 17:28
  • I making that array, because i sending that to client(JS), using JSON. Thank you Mark Baker - I exacly was asking, what you said :) I need just 1 call to DB, not 4(or more). I wasnt sure, so I decide to ask. – Xaller Sep 24 '17 at 17:38

1 Answers1

0

Option 1 is making you call the database four times to get the exact same information you got in the first call.

Think logically, how is doing the same thing four times quicker than doing it once?

Option 2 is indeed the fastest, however, you don't even need to store the result of in $myArray because you already have that data in $mapInfo.

The only benefit that $myArray has is having an "easier to read" key for each value, the keys are already easy enough to read.

EDIT: As said in the comments you are using a deprecated function to call to your database. Do look at using PDO prepared statements to avoid SQL injection.

MinistryOfChaps
  • 1,458
  • 18
  • 31