我的代码中有一些错误,例如调用未定义方法mysqli_stmt :: get_result

时间:2019-06-21 09:01:14

标签: php

  

在服务器上调用未定义的方法mysqli_stmt :: get_result但在本地   服务器工作正常

DBController.php

class DBController
{
    private $host     = "localhost";
    private $user     = "xxxx";
    private $password = "xxxxx";
    private $database = "xxxx";
    private static $conn;
    public function __construct()
    {
        $this->conn = mysqli_connect($this->host, $this->user, $this->password, $this->database);
    }
    public static function getConnection()
    {
        if (empty($this->conn)) {
            new Database();
        }
    }
    public function getDBResult($query, $params = [])
    {
        $sql_statement = $this->conn->prepare($query);
        if (!empty($params)) {
            $this->bindParams($sql_statement, $params);
        }
        $sql_statement->execute();
        $result = $sql_statement->get_result();
        var_dump($result);
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $resultset[] = $row;
            }
        }
        if (!empty($resultset)) {
            return $resultset;
        }
    }
    public function updateDB($query, $params = [])
    {
        $sql_statement = $this->conn->prepare($query);
        if (!empty($params)) {
            $this->bindParams($sql_statement, $params);
        }
        $sql_statement->execute();
    }
    public function bindParams($sql_statement, $params)
    {
        $param_type = "";
        foreach ($params as $query_param) {
            $param_type .= $query_param["param_type"];
        }
        $bind_params[] = &$param_type;
        foreach ($params as $k => $query_param) {
            $bind_params[] = &$params[$k]["param_value"];
        }
        call_user_func_array([
            $sql_statement,
            'bind_param',
        ], $bind_params);
    }
}

ShoppingCart.php

require_once "DBController.php";
class ShoppingCart extends DBController
{
    public function getAllProduct()
    {
        $query         = "SELECT * FROM product";
        $productResult = $this->getDBResult($query);
        return $productResult;
    }
    public function getMemberCartItem($user_id)
    {
        $query = "SELECT product.*, add_product.id as id,add_product.pro_value,add_product.pro_price FROM product, add_product WHERE
            product.pro_id = add_product.product_id AND add_product.user_id = ?";
        $params = [
            [
                "param_type"  => "i",
                "param_value" => $user_id,
            ],
        ];
        $cartResult = $this->getDBResult($query, $params);
        return $cartResult;
    }
    public function getProductByCode($product_code)
    {
        $query  = "SELECT * FROM product WHERE pro_id=?";
        $params = [
            [
                "param_type"  => "s",
                "param_value" => $product_code,
            ],
        ];
        $productResult = $this->getDBResult($query, $params);
        return $productResult;
    }
    public function getProductById($user_id, $product_id)
    {
        $query  = "SELECT * FROM product WHERE pro_id=?";
        $params = [
            [
                "param_type"  => "s",
                "param_value" => $product_code,
            ],
        ];
        $productResult = $this->getDBResult($query, $params);
        return $productResult;
    }
    public function getCartItemByProduct($product_id, $user_id)
    {
        $query  = "SELECT * FROM add_product WHERE product_id = ? AND user_id = ?";
        $params = [
            [
                "param_type"  => "i",
                "param_value" => $product_id,
            ],
            [
                "param_type"  => "i",
                "param_value" => $user_id,
            ],
        ];
        $cartResult = $this->getDBResult($query, $params);
        return $cartResult;
    }
    public function addToCart($user_id, $product_id, $add_type, $pro_value, $date_time, $fdate, $ftime, $pro_price)
    {
        $query  = "INSERT INTO add_product (user_id,product_id,add_type,pro_value,date_time,fdate,ftime,pro_price) VALUES (?, ?, ?, ?, ?, ?, ?,?)";
        $params = [
            [
                "param_type"  => "i",
                "param_value" => $user_id,
            ],
            [
                "param_type"  => "i",
                "param_value" => $product_id,
            ],
            [
                "param_type"  => "i",
                "param_value" => $add_type,
            ],
            [
                "param_type"  => "i",
                "param_value" => $pro_value,
            ],
            [
                "param_type"  => "i",
                "param_value" => $date_time,
            ],
            [
                "param_type"  => "i",
                "param_value" => $fdate,
            ],
            [
                "param_type"  => "i",
                "param_value" => $ftime,
            ],
            [
                "param_type"  => "i",
                "param_value" => $pro_price,
            ],
        ];
        $this->updateDB($query, $params);
    }
    public function updateCartQuantity($pro_value, $id)
    {
        $query  = "UPDATE add_product SET  pro_value = ? WHERE id= ? ";
        $params = [
            [
                "param_type"  => "i",
                "param_value" => $pro_value,
            ],
            [
                "param_type"  => "i",
                "param_value" => $id,
            ],
        ];
        $this->updateDB($query, $params);
    }
    public function deleteCartItem($cart_id)
    {
        $query  = "DELETE FROM add_product WHERE id = ?";
        $params = [
            [
                "param_type"  => "i",
                "param_value" => $cart_id,
            ],
        ];
        $this->updateDB($query, $params);
    }
    public function emptyCart($user_id)
    {
        $query  = "DELETE FROM add_product WHERE user_id = ?";
        $params = [
            [
                "param_type"  => "i",
                "param_value" => $user_id,
            ],
        ];
        $this->updateDB($query, $params);
    }
}

0 个答案:

没有答案