2015-05-12 112 views
3

我有三個文件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正在消耗更多的任務。

+0

你在調用DB_Connect :: close();在所有? – Christoph

+0

不,我應該在DB_Function析構函數中使用它嗎? – pratz9999

+0

hm即使沒有您的交互,PHP通常應該在腳本結尾關閉連接。你能發佈你的實際查詢嗎? $ db-> getUserByEmailAndPassword($ email,$ password);和$ db-> getUserByuserid($ uservalue); – Christoph

回答

1

這裏是我的建議

1.-始終關閉檢索數據後的連接。

2:如果你期望只有一行像這樣

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

查詢,你應該在查詢的末尾添加LIMIT 1檢索唯一可能的排

3,添加索引表

4.-用mysqlslap測試你的性能