简体   繁体   中英

How to add SQL where and limit query in PHP?

I want to show table order where status = 'confirmed' , but my code doesn't work:

public function getdataorderconfirmed($limit, $offset){
    if($offset == '') {
        $sql = $this->db->query**("SELECT * FROM orderan where status = 'confirmed' LIMIT '.$limit.' 
            OFFSET 0 ");**
    return $sql->result();
    }
    else{
        **$sql = $this->db->query("SELECT * FROM orderan where status = 'confirmed' LIMIT '.$limit.' 
        OFFSET '.$offset.'");**
    return $sql->result();
    }
}

The LIMIT and OFFSET don't need their values quoted because they're numbers, so remove the single quotes around them here:

$sql = $this->db->query("SELECT * FROM orderan where status = 'confirmed'
                         LIMIT $limit OFFSET 0");

And here:

$sql = $this->db->query("SELECT * FROM orderan where status = 'confirmed'
                         LIMIT $limit OFFSET $offset");

Change the query as following, you can use PHP variables as it is if in double quotes, there is no need to concat:

public function getdataorderconfirmed($limit, $offset){
            if($offset == '') {
                $sql = $this->db->query**("SELECT * FROM orderan where status = 'confirmed' LIMIT $limit OFFSET 0");**
            return $sql->result();
            }
            else{
                **$sql = $this->db->query("SELECT * FROM orderan where status = 'confirmed' LIMIT $limit OFFSET $offset");**
            return $sql->result();
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM