dongnuo2879 2019-02-05 04:12
浏览 384

在多个表中使用关键字进行搜索

I am trying to set up my search bar to create a table of results when searching through two tables.

Each table (fresh, salt) has 2 columns (ID and DESCRIPTION).

if(isset($_REQUEST["term"])){
// Prepare a select statement
$term = $_REQUEST['term'];

     $sql = "SELECT * FROM fresh  WHERE 
     description LIKE '%{$term}%' or 
     sku like '%{$term}%'
     union
     SELECT * FROM marine WHERE 
     description LIKE '%{$term}%' or 
     sku like '%{$term}%'";

        if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_term);

        // Set parameters
        $param_term = $_REQUEST["term"] . '%';

Searched terms can be anything like, "Neon Tetra", but the search currently looks for that string as is and I'd like for it to search as two+ different keywords separated by space, even if terms are backward such as "Tetra Neon".

  • 写回答

1条回答 默认 最新

  • dpgbh20688 2019-02-05 04:37
    关注

    which id relatating with both table use that.i m just giving example.don't know which table's have primary key or secondary. try this:

    $this->db->join('fresh', 'fresh.id = salt.freshid')
                    ->select('fresh.*,salt.id');
    

    //you can all fields from salt table using salt.id,salt.name etc.

    $where = '';
        if ($this->input->post('search')['value'] && $this->input->post('search')['value'] != '') {
            $where = "(fresh.sku LIKE '%" . $this->input->post('search')['value'] . "%' OR saly.sku LIKE '%" . $this->input->post('search')['value']. "%')";
        }
        if ($where != '') {
            $this->db->where($where);
        }
        $count = $this->db->get('salt')->num_rows();
        if ($this->input->post('length') != '-1') {
            $this->db->limit($this->input->post('length'), $this->input->post('start'));
        }
        $query = $this->db->get('salt')->result_array();
        return [$query, $count];
    
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?