php - Memory Leak or Connection not closed -
i having 3 files index.php, db_function, db_connect connect through mysql server. response slow , task running according hosting server people.
index.php
if (isset($_post['tag']) && $_post['tag'] != '') { // 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 tag type if ($tag == 'login') { // request type check login $email = $_post['email']; $password = $_post['password']; // check user $user = $db->getuserbyemailandpassword($email, $password); if ($user != false) { // user found // echo json 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 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 code here // constructor function __construct() { require_once 'db_connect.php'; // connecting database $this->db = new db_connect(); $this->db->connect(); } // destructor function __destruct() { } /** * user email , password */ public function getuserbyemailandpassword($email, $password) { $result = mysql_query("select * userdetails email = '$email' , password = '$password'") or die(mysql_error()); // check 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; } } /** * user email , password */ public function getuserbyuserid($uservalue) { $result = mysql_query("select * calorieinfo userid= '$uservalue' ") or die(mysql_error()); // check 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 calorieinfo( userid,startdate, createddate, updateddate,createdby,updatedby) values('$uservalue' ,now(), now(), now(),'$uservalue','$uservalue')"); if ($calresult) { $id = mysql_insert_id(); $calresult = mysql_query("select * calorieinfo 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 database public function connect() { require_once 'config.php'; // connecting 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 more optimized should aware of ? missing in code. hosting people says tomcat , mysql consuming more task.
here recommendations
1.- close connection after retrieving data.
2.- if expecting 1 row query
"select * userdetails email = '$email' , password = '$password'
you should add limit 1
@ end of query retrieving possible row
3.-add indexes table
4.-test performance mysqlslap
Comments
Post a Comment