php - 内存泄漏或连接未关闭

标签 php mysql database

我有三个文件 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();
    }

}

?>

有什么我应该注意的更优化的吗? 我的代码中缺少什么。托管人说tomcat和mysql比较耗时。

最佳答案

这是我的建议

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

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

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

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

3.-给表添加索引

4.-使用 mysqlslap 测试你的性能

关于php - 内存泄漏或连接未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30182485/

相关文章:

php - 如何获取mysql行中包含字符串的列名

php - 将 PHP 5.6 代码迁移到 PHP 7.3 的正确步骤

php - 将mysql查询结果存储在数组中并在另一个查询中使用该数据

php - Magento CE 2.0 安装在大约 90% 时停止

mysql - 带有辅助列的 SQL 语句联合

MySQL 查询选择不同字段然后按日期排序

php - 在循环外创建查询

Android - 子 Activity 在 onDestroy 上更新数据库, parent 在 onResume 上读取

mysql - 如何对两个表进行分组并应用聚合函数?

javascript - 针对轻量级、可移植的基于 JS 的 Web 应用程序存储持久数据的最佳方式的建议