Klipp Ohei
Klipp Ohei

Reputation: 385

Performance of function which connects to database

I have a function to check wether a function/module is disabled by an admin or not.

The function looks like this (in functions.php):

function function_enabled($function_module) {
    include("dbconnect.php");
    if ($function_module == "changelogs" OR $function_module == "news" OR $function_module == "calendar" OR $function_module == "groups" OR $function_module == "settings" OR $function_module == "eat" OR $function_module == "weather" OR $function_module == "usertext" OR $function_module == "login") {
        $sql = $db->query("SELECT enabled FROM functions WHERE function_name = '$function_module'");
        $row = $sql->fetch_object();
        $return = $row->enabled;
        return $return;
    }
}

And this is how I use the function (in any other php file):

include("functions.php");
if(function_enabled("weather") == 1) {
    //do the weather stuff here
} else {
    echo"Function disabled";
}

Now my question: How is the performance of this function? Can this cause relative high performance hits? Is there a better/faster way to check wether a function is enabled or not or is this script okay?

UPDATE 1

Now, I combined these multiple queries into one so that all values are in an array.

function compare_id($a, $b) {
    if ($a['id'] == $b['id']) return 0;
    return ($a['id'] < $b['id']) ? -1 : 1;
}   

$sql = $db->query("SELECT function_name, enabled, id FROM functions");

$function_array = array();
while ($row = mysqli_fetch_assoc($sql)) {
    $function_array[] = $row;
}
usort($function_array, compare_id);

if($function_array[0]['enabled'] == 1) {
    echo"changelogs enabled<br>";
} else {
    echo"changelogs disabled<br>";
}

if($function_array[2]['enabled'] == 1) {
    echo"calendar enabled<br>";
} else {
    echo"calendar disabled<br>";
}

if($function_array[7]['enabled'] == 1) {
    echo"usertext enabled<br>";
} else {
    echo"usertext disabled<br>";
}

if($function_array[8]['enabled'] == 1) {
    echo"login enabled<br>";
} else {
    echo"login disabled<br>";
}

if($function_array[4]['enabled'] == 1) {
    echo"settings enabled<br>";
} else {
    echo"settings disabled<br>";
}

Is this method faster?

Upvotes: 0

Views: 49

Answers (2)

Halayem Anis
Halayem Anis

Reputation: 7805

function function_enabled($function_module) {
    $_FUNCTION_MODULE = array ("changelogs", "news", "calendar", "groups", "settings", "eat", "weather", "usertext", "login");
    require_once ("dbconnect.php");
    if (in_array($function_module, $_FUNCTION_MODULE) {
        $query      = $db->query("SELECT enabled FROM functions WHERE function_name = '$function_module'");
        $row            = $query->fetch_object();
        $isEnabled  = $row->enabled;
        return $isEnabled;
    }
    return NULL;
}

Upvotes: 1

Lex
Lex

Reputation: 5014

It looks OK. Is it giving you issues? There could be performance issues in the DB query from the table or index.

Generally you shouldn't build for performance from the start. Rather build something that functions, then go back an optmise where the choke points are. There are high load webservices which use simple frameworks (Ruby on Rails, Laravel) They only have to optimise 2-5% of the code where the load is. When you know where the choke points are and why its choking you can make an educated choice on how to optmise. Should it be optimised, or should I rewite this in another language.

Does your functions table change fequently? If not you could possibly export it to file. Then PHP could read it once and store it in Memory as an array. Referencing an array would be a lot faster functions['weather'] == 1 with no DB throughput issues.

Upvotes: 1

Related Questions