Php类内部连接关闭或类关闭?

时间:2016-02-18 13:09:18

标签: php function class pdo

我有php类USERDatabase类我正在做10000 users app,该用户将每天查询3-4 times最小值。我的班级基础和功能基础在这里他们正在寻找那些have any problem ?或需要any fix ?

MY db class

class Database
{   
    private $host = "";
    private $db_name = "";
    private $username = "";
    private $password = "";
    public $conn;

    public function dbConnection()
    {

        $this->conn = null;    
        try
        {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
        }
        catch(PDOException $exception)
        {

             print'{"success": "0","message": "Service Error, Please try again later "}';

        }


        return $this->conn;
    }


}

Also my USER class here

<?php

require_once('dbconfig.php');

class USER
{   

    private $conn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
    }

    public function runQuery($sql)
    {
        $stmt = $this->conn->prepare($sql);
        return $stmt;
    }

    public function updateProduct($name,$code,$description,$quantity,$price,$specialwarning,$productID)
    {
        try
        {


            $stmt2 = $this->conn->prepare("UPDATE Products SET productCode='$code',productName='$name',productDescription='$description',specialWarning='$specialwarning',productQuantity='$quantity',productPrice='$price' WHERE ID=$productID");
            $stmt2->execute();

            echo'{"success": "1", "message": "Product Updated"}';


            return true;


        }
            catch(PDOException $e)
        {



    }
     $this->conn = null;  // HERE I DID NULL FOR DISABLE MAX CONNECTIONS
}

}

AND HER MY updateproduct.php

<?php
header('Content-type: application/json');

$name = $_REQUEST['name'];
$code = $_REQUEST['code'];
$description = $_REQUEST['description'];
$quantity = $_REQUEST['quantity'];
$price = $_REQUEST['price'];
$specialwarning = $_REQUEST['specialwarning'];
$productID = $_REQUEST['productID'];



include_once 'class.user.php';

$user = new USER($DB_con);

if($user->updateProduct($name,$code,$description,$quantity,$price,$specialwarning,$productID))
 {

}
 else
 {

 } 

?>

您可以检查我的代码是good class big users ?和良好连接returns ?我可以添加一些内容吗?或需要any fix ?还需要close class ?

由于

0 个答案:

没有答案