内存泄漏或连接未关闭

时间:2023-01-20 17:22:09

I am having three files index.php, DB_Function, DB_Connect to connect through mysql server. But response is very slow and task is almost running according to the hosting server people.

我有三个文件index.php,DB_Function,DB_Connect通过mysql服务器连接。但响应非常缓慢,根据托管服务器人员的任务几乎正在运行。

index.php

    if (isset($_POST['tag']) && $_POST['tag'] != '') {
    // get tag
    $tag = $_POST['tag'];

    // include db handler
    require_once 'DB_Functions.php';
    $db = new DB_Functions();

    // response Array
    $response = array("tag" => $tag, "success" => 0, "error" => 0);

    // check for tag type
    if ($tag == 'login') {
        // Request type is check Login
        $email = $_POST['email'];
        $password = $_POST['password'];

        // check for user
        $user = $db->getUserByEmailAndPassword($email, $password);

               if ($user != false) {
                // user found
                // echo json with success = 1
                $uservalue= $user["userid"];
                $usercal = $db->getUserByuserid($uservalue);
                 if ($usercal != false) {
                   $response["usercal"]["userid"] = $usercal["userid"];
                   $response["usercal"]["newcalorie"] = $usercal["newcalorie"];
                   $response["usercal"]["oldcalorie"] = $usercal["oldcalorie"];
                   $response["usercal"]["flag"] = $usercal["flag"];
                   $response["usercal"]["fat"] = $usercal["fat"];
                   $response["usercal"]["carbohydrate"] = $usercal["carbohydrate"];
                   $response["usercal"]["protein"] = $usercal["protein"];
                   $response["usercal"]["startdate"] = $usercal["startdate"];
                   $response["usercal"]["enddate"] = $usercal["enddate"];
                   $response["usercal"]["createddate"] = $usercal["createddate"];
                   $response["usercal"]["updateddate"] = $usercal["updateddate"];
                   $response["usercal"]["createdby"] = $usercal["createdby"];
                   $response["usercal"]["updatedby"] = $usercal["updatedby"];

                 }                       


                $response["success"] = 1;
                $response["user"]["userid"] = $user["userid"];
                $response["user"]["fname"] = $user["fname"];
                $response["user"]["email"] = $user["email"];
                $response["user"]["altemail"] = $user["altemail"];
                $response["user"]["age"] = $user["age"];
                $response["user"]["gender"] = $user["gender"];
                $response["user"]["weight"] = $user["weight"];
                $response["user"]["unit"] = $user["unit"];
                $response["user"]["height"] = $user["height"];
                $response["user"]["weightgoal"] = $user["weightgoal"];
                $response["user"]["activitylevel"] = $user["activitylevel"];
                $response["user"]["exerciselevel"] = $user["exerciselevel"];
                $response["user"]["disease"] = $user["disease"];
                $response["user"]["createddate"] = $user["createddate"];
                $response["user"]["updateddate"] = $user["updateddate"];
                $response["user"]["createdby"] = $user["createdby"];
                $response["user"]["updatedby"] = $user["updatedby"];

                echo json_encode($response);         

          } else {
            // user not found
            // echo json with error = 1
            $response["error"] = 1;
            $response["error_msg"] = "Incorrect email or password!";
            echo json_encode($response);
        }
    }
 else {
    echo "Access Denied";
}
?>

DB_Function.php

    <?php

class DB_Functions {

    private $db;

    //put your code here
    // constructor
    function __construct() {
        require_once 'DB_Connect.php';
        // connecting to database
        $this->db = new DB_Connect();
        $this->db->connect();
    }

// destructor
    function __destruct() {

    }

 /**
 * Get user by email and password
 */
public function getUserByEmailAndPassword($email, $password) {
    $result = mysql_query("SELECT * FROM userDetails  WHERE email = '$email' AND password = '$password'") or die(mysql_error());
    // check for result 
    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) {
        $result = mysql_fetch_array($result);
            return $result;

    } else {
        // user not found
        return false;
    }
}

 /**
 * Get user by email and password
 */
public function getUserByuserid($uservalue) {
    $result = mysql_query("SELECT * FROM CalorieInfo  WHERE userid= '$uservalue' ") or die(mysql_error());
    // check for result 
    $no_of_rows = mysql_num_rows($result);
    if ($no_of_rows > 0) {
        $result = mysql_fetch_array($result);

            return $result;

    } else {

    $calresult = mysql_query("INSERT INTO CalorieInfo( userid,startdate, createddate, updateddate,createdby,updatedby) VALUES('$uservalue' ,NOW(), NOW(), NOW(),'$uservalue','$uservalue')");

     if ($calresult) {
        $id = mysql_insert_id();
        $calresult = mysql_query("SELECT * FROM CalorieInfo WHERE id = $id");
        return mysql_fetch_array($calresult);
    }else{
        // user not found
        return false;
        }
    }
}


?>

DB_Connect.php

<?php
class DB_Connect {

    // constructor
    function __construct() {
       //this->connect();
    }

    // destructor
    function __destruct() {
    //closing db 

    }

    // Connecting to database
    public function connect() {
        require_once 'config.php';
        // connecting to mysql
        $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
        // selecting database

        mysql_select_db(DB_DATABASE) or die(mysql_error());

        // return database handler
        return $con;
    }

    // Closing database connection
    public function close() {
        mysql_close();
    }

}

?>

Is there anything more optimized that i should be aware of ? Is something missing in my code. Hosting people says that tomcat and mysql is consuming more task.

我应该注意哪些更优化的东西?我的代码中缺少一些东西。主持人说tomcat和mysql正在消耗更多的任务。

1 个解决方案

#1


Here are my recommendations

这是我的建议

1.- Always close the connection after retrieving the data.

1.-检索数据后始终关闭连接。

2.- If you are expecting just one row for a query like this

2.-如果您希望这样的查询只有一行

"SELECT * FROM userDetails  WHERE email = '$email' AND password = '$password' 

you should add LIMIT 1 at the end of the query for retrieving the only possible row

您应该在查询末尾添加LIMIT 1以检索唯一可能的行

3.-Add indexes to the table

3.-向表中添加索引

4.-Test your performance with mysqlslap

4.-使用mysqlslap测试你的表现

#1


Here are my recommendations

这是我的建议

1.- Always close the connection after retrieving the data.

1.-检索数据后始终关闭连接。

2.- If you are expecting just one row for a query like this

2.-如果您希望这样的查询只有一行

"SELECT * FROM userDetails  WHERE email = '$email' AND password = '$password' 

you should add LIMIT 1 at the end of the query for retrieving the only possible row

您应该在查询末尾添加LIMIT 1以检索唯一可能的行

3.-Add indexes to the table

3.-向表中添加索引

4.-Test your performance with mysqlslap

4.-使用mysqlslap测试你的表现