【问题标题】:Codeiginiter get all rows from mysql database that start with tomCodeigniter从mysql数据库中获取所有以tom开头的行
【发布时间】:2022-06-16 20:08:22
【问题描述】:

我正在尝试使用 codeigniter 获取 mysql 数据库中以字符“tom”开头的所有行,但结果与我想要的不匹配

我的代码

控制器

public function get_merchant_name(){

    $names = $this->model_merchants->getMerchantName("tom");
    print_r($names);
}

型号

public function getMerchantName($value)
{
    $sql = "SELECT first_name, last_name FROM tbl_merchants WHERE first_name like  ? or  last_name like  ? or other_names like  ?";
    $query = $this->db->query($sql, array("%$value", "%$value", "%$value"));
    return  $query->result_array() ;

}

【问题讨论】:

  • 您的查询正在寻找以tom 结束 的值。您应该将绑定更改为"$value%"

标签: php mysql codeigniter


【解决方案1】:

你可以试试这个

public function getMerchantName($value){
    $this->db->select('*');
    if ($value) {
        $this->db->group_start();
        $this->db->like('first_name', $value);
        $this->db->group_end();
    }
    $query = $this->db->get('tbl_merchants');
    return $query->result();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 2012-10-31
    • 2016-06-28
    • 2016-09-14
    • 2021-04-08
    • 1970-01-01
    相关资源
    最近更新 更多