Php 如何使用DB静态连接进行分页连接

Php 如何使用DB静态连接进行分页连接,php,pagination,Php,Pagination,我已经为我的产品页面创建了分页!同时写下代码。我犯了这个错误 严格标准:不应在第147行的D:\wamp\www\project\ali\models\product.php中静态调用非静态方法DB\u Connection::obj\u DB() require_once 'db_connection.php'; require_once 'brand.php'; class Product extends DB_Connection{ public $productID; public $

我已经为我的产品页面创建了分页!同时写下代码。我犯了这个错误

严格标准:不应在第147行的D:\wamp\www\project\ali\models\product.php中静态调用非静态方法DB\u Connection::obj\u DB()

require_once 'db_connection.php';
require_once 'brand.php';
class Product extends DB_Connection{
public $productID;
public $product_name;
public $description;
public $product_features;
public $unit_price;
public $quantity;
public $view_count;
public $product_image;
public $featured;

public $brand;


public function __construct() {
    $this->brand = new Brand();//composition
}
public function get_product($view_update = FALSE) {

    $obj_db = $this->obj_db();


    $query = "select * "
            . "from products_full "
            . "where productID = '$this->productID'";


    $result = $obj_db->query($query);

    if($obj_db->errno){

        throw new Exception("Select product Error - $obj_db->error -$obj_db->errno");
    }

    if($result->num_rows == 0){
        throw new Exception("Product not found");
    }

    $data = $result->fetch_object();

    if($view_update){
        $data->view_count++;


        $query_update = "update products set "
                . "view_count = view_count+1 "
                . "where `productID` = '$this->productID'";

        $r2 = $obj_db->query($query_update);
        if($obj_db->affected_rows == 0){

        }
    }

        $this->productID = $data->productID;
        $this->product_name = $data->product_name;
        $this->description = $data->description;
        $this->product_features = $data->product_features;
        $this->product_image = $data->product_image;
        $this->unit_price = $data->unit_price;
        $this->quantity = $data->quantity;
        $this->view_count = $data->view_count;
        $this->brand->brandID = $data->brandID;
        $this->brand->brand_name = $data->brand_name;
        $this->brand->brand_image = $data->brand_image;
}    
public static function get_products($start = -1, $count = 0, $type = "all", $brandID = 0) {

    //$obj_db = $this->obj_db();
    $obj_db = self::obj_db();

    $query = "select * from `products` ";

    if($brandID > 0){
        $query .= " where brandID = $brandID";
    }

    $types = array("all", "top", "new");

    if(!in_array($type, $types)){
        $type = "all";
    }
    if($type == "top"){
        $query .= " order by view_count desc";
    }

    if($type == "new"){
        $query .= " order by productID desc";
    }

    if($start > -1 && $count > 0){
        $query .= " limit $start, $count";
    }


    $result = $obj_db->query($query);

    if($obj_db->errno){

        throw new Exception("Select products Error - $obj_db->error -$obj_db->errno");
    }

    if($result->num_rows == 0){
        throw new Exception("Product(s) not found");
    }

    $products = array();
    while($data = $result->fetch_object()){

        $temp = new Product();
        $temp->productID = $data->productID;
        $temp->product_name = $data->product_name;
        $temp->description = $data->description;
        $temp->product_features = $data->product_features;
        $temp->product_image = $data->product_image;
        $temp->unit_price = $data->unit_price;
        $temp->quantity = $data->quantity;
        $temp->view_count = $data->view_count;
        //$temp->brand = TRUE;
        $products[] = $temp;
    } 

    return $products;
}

public static function pagination($item_per_page = 6, $brandID = 0){
    $obj_db = self::obj_db();

    $query = "select count(*) 'count' from `products`";//alias

    if($brandID > 0){
        $query .= " where brandID = $brandID";
    }

    $result = $obj_db->query($query);


    if($result->num_rows == 0){
        throw new Exception("Product(s) not found");
    }

    $data = $result->fetch_object();
    $total_items = $data->count;

    $page_count = ceil($total_items/$item_per_page);

    $page_nums = array();

    for($i = 1, $j = 0 ; $i <= $page_count; $i++, $j+=$item_per_page){
        $page_nums[$i] = $j;
    }

    return $page_nums;
}
require_once'db_connection.php';
需要一次'brand.php';
类产品扩展了DB_连接{
公共$productID;
公共$产品名称;
公共说明;
公共$产品功能;
公帑$单价;
公共数量;
公众$view_计数;
公众形象;;
公共服务;;
公共品牌;
公共函数构造(){
$this->brand=新品牌();//组合
}
公共函数get\u product($view\u update=FALSE){
$obj_db=$this->obj_db();
$query=“选择*”
“从产品到全部”
“其中productID='$this->productID';
$result=$obj_db->query($query);
如果($obj_db->errno){
抛出新异常(“选择产品错误-$obj_db->错误-$obj_db->错误号”);
}
如果($result->num_rows==0){
抛出新异常(“未找到产品”);
}
$data=$result->fetch_object();
如果($view\u update){
$data->view_count++;
$query\u update=“更新产品集”
“查看次数=查看次数+1”
“其中`productID`='$this->productID';
$r2=$obj\u db->query($query\u update);
如果($obj\U db->受影响的\U行==0){
}
}
$this->productID=$data->productID;
$this->product\u name=$data->product\u name;
$this->description=$data->description;
$this->product\u features=$data->product\u features;
$this->product\u image=$data->product\u image;
$this->unit\u price=$data->unit\u price;
$this->quantity=$data->quantity;
$this->view\u count=$data->view\u count;
$this->brand->brandID=$data->brandID;
$this->brand->brand\u name=$data->brand\u name;
$this->brand->brand\u image=$data->brand\u image;
}    
公共静态函数get_products($start=-1、$count=0、$type=“all”、$brandID=0){
//$obj_db=$this->obj_db();
$obj_db=self::obj_db();
$query=“从‘产品’中选择*”;
如果($brandID>0){
$query.=“其中brandID=$brandID”;
}
$types=数组(“全部”、“顶部”、“新”);
if(!in_数组($type,$types)){
$type=“全部”;
}
如果($type==“top”){
$query.=“按视图的订单数量描述”;
}
如果($type==“new”){
$query.=“按产品ID描述的订单”;
}
如果($start>-1&&$count>0){
$query.=“限制$start,$count”;
}
$result=$obj_db->query($query);
如果($obj_db->errno){
抛出新异常(“选择产品错误-$obj_db->错误-$obj_db->错误号”);
}
如果($result->num_rows==0){
抛出新异常(“未找到产品”);
}
$products=array();
而($data=$result->fetch_object()){
$temp=新产品();
$temp->productID=$data->productID;
$temp->product\U name=$data->product\U name;
$temp->description=$data->description;
$temp->product\U features=$data->product\U features;
$temp->product\U image=$data->product\U image;
$temp->单价=$data->单价;
$temp->quantity=$data->quantity;
$temp->view\u count=$data->view\u count;
//$temp->brand=TRUE;
$products[]=$temp;
} 
退回$products;
}
公共静态函数分页($item_per_page=6,$brandID=0){
$obj_db=self::obj_db();
$query=“选择count(*)`products``中的'count';//别名
如果($brandID>0){
$query.=“其中brandID=$brandID”;
}
$result=$obj_db->query($query);
如果($result->num_rows==0){
抛出新异常(“未找到产品”);
}
$data=$result->fetch_object();
$total_items=$data->count;
$page\u count=ceil($total\u items/$item\u per\u page);
$page_nums=array();

对于($i=1,$j=0;$i首先让我给你一个例子,并解释为什么会发生这种情况

class A{
    public function foo(){
        echo "foo";
    }

    public static function bar(){
        // If you call the foo function like this:
        // error: Strict standards: Non-static method A::foo() should not be called statically
        //self::foo();

        // That means it should be called like this:
        (new A)->foo();
    }
}

A::bar();
静态
非静态
之间的区别在于,第一个类不需要初始化,因此您可以调用
类名
,然后向其追加
,并立即调用该方法。如下所示:

ClassName::method();
$obj = new ClassNmae();
$onj->method();
如果该方法不是静态的,则需要如下初始化:

ClassName::method();
$obj = new ClassNmae();
$onj->method();
但是,在PHP 5.4中,您可以使用此语法来加快调用速度:

(new ClassName)->method(); 
也就是说,您需要在代码中更改以下内容:

// your code

public function get_product($view_update = FALSE) {

    $obj_db = $this->obj_db();

    // your code
}    
public static function get_products($start = -1, $count = 0, $type = "all", $brandID = 0) {

    $obj_db = (new DB_Connection)->obj_db();

    // your code
}

public static function pagination($item_per_page = 6, $brandID = 0){

    $obj_db = (new DB_Connection)->obj_db();

    // your code
}

我犯了一个错误,我创建了用于分页的静态函数。我忘记在db连接页面上添加静态

protected static function obj_db(){


感谢所有指导我的人!

根据您的截图,问题在于,
$obj_db=self::obj_db();
。您试图将实例方法作为静态方法调用。首先创建一个对象,然后调用
obj_db()
方法。它是由Paul创建的,$obj_db=$this->obj_db();/*$query=“选择产品。*,brands.brand\u name,brands.brand\u image”“从产品”“左加入品牌”“在产品上。brandID=brands.brandID”“和产品。productID='$this->productID'”;***/$query=“选择*”“from products_full”。“其中productID='$this->productID'”;$result=$obj_db->query($query);用更新的代码编辑您的问题。我已经更新了产品类代码,非常感谢您的友好回答。您现在遇到了什么错误?我明白您的意思,但我使用了“代码”抽象类DB_连接{受保护函数obj_DB(){DB connection页面中的'code'。执行建议的更改后,出现如下错误:无法在第86行的D:\wamp\www\project\ali\models\product.php中实例化抽象类DB\u connection,这是bec