簡體   English   中英

使用的 SELECT 語句在 codeigniter 中具有不同的列數

[英]The used SELECT statements have a different number of columns in codeigniter

錯誤編號:1222
使用的 SELECT 語句具有不同的列數

這是控制器

    <?php
    class Autocomplete extends CI_Controller{
        function __construct() {
            parent::__construct();
            $this->load->model('datacomplete');
        }

        public function index(){
            $this->load->view('view_demo');
        }
        public function GetCountryName(){
            $keyword=$this->input->post('keyword');
            $data=$this->datacomplete->GetRow($keyword); 

            echo json_encode($data);
        }

    }
    ?>

這是模型

        <?php
    class Datacomplete extends CI_Model{

        public function GetRow($keyword) {        
           $this->db->select('collg_name,city,state,country as type');
           $this->db->from('tbl_college');
           $this->db->like("collg_name",$keyword);
           $this->db->or_like('city',$keyword,'after');
           $this->db->or_like('state',$keyword,'after');
           $this->db->or_like('country',$keyword,'after');
           $query1 = $this->db->get_compiled_select();
           $this->db->select('course_offrd_name,category_name,subcategory_name');
           $this->db->from('tbl_course_offered');
           $this->db->like("course_offrd_name",$keyword);
           $this->db->or_like('category_name',$keyword,'after');
           $this->db->or_like('subcategory_name',$keyword,'after');
           $query2 = $this->db->get_compiled_select();
           $result = $this->db->query($query1." UNION ".$query2);
           return $result->result();
        }
    }

這是視圖

    <!DOCTYPE html>
    <html>
        <head>
            <!-- Latest compiled and minified CSS -->
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
            <!-- Latest compiled and minified JavaScript -->
            <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <!-- Latest compiled and minified JavaScript -->
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
            <script src="<?php echo base_url(); ?>assets/custom.js"></script>
        </head>
        <body style="background-color: #000000;">
            <div class="row">
            <center><h2 style="color: #fff;">AUTOCOMPLETE FORM FROM DATABASE USING CODEIGNITER AND AJAX</h2></center>
                <div class="col-md-4 col-md-offset-4" style="margin-top: 200px;">

                        <label class="control-lable" style="color: #fff;">Country Name</label>
                        <input style="height:70px" type="text" id="country" autocomplete="off" name="country" class="form-control" placeholder="Type to get an Ajax call of Countries">        
                        <ul class="dropdown-menu txtcountry" style="margin-left:15px;margin-right:0px;" role="menu" aria-labelledby="dropdownMenu"  id="DropdownCountry"></ul>
    </div>
            </div>
        </body>
    </html>

    This is custom.js

    $(document).ready(function () {
        $("#country").keyup(function () {
            $.ajax({
                type: "POST",
                url: "http://localhost/codeajax/autocomplete/GetCountryName",
                data: {
                    keyword: $("#country").val()
                },
                dataType: "json",
                success: function (data) {

                    if (data.length > 0) {
                        $('#DropdownCountry').empty();
                        $('#country').attr("data-toggle", "dropdown");
                        $('#DropdownCountry').dropdown('toggle');
                    }
                    else if (data.length == 0) {
                        $('#country').attr("data-toggle", "");
                    }
                    $.each(data, function (key,value) {
                        if (data.length >= 0)
                            $('#DropdownCountry').append('<li role="displayCountries" ><a role="menuitem dropdownCountryli" class="dropdownlivalue">' + value['name'] + '</a></li>');
                    });
                }
            });
        });
        $('ul.txtcountry').on('click', 'li a', function () {
            $('#country').val($(this).text());
        });
    });

我在這里嘗試使用 codeigniter 和 ajax 從三個表中搜索關鍵字。
我在model遇到了那個錯誤

我該如何解決這個問題?

另外,如果問題得到解決,我的搜索是否會從數據庫中獲取數據。 代碼有什么問題?

您必須在select語句中使用相同的列號,如下所示

首先select您使用 4 列:

$this->db->select('collg_name,city,state,country as type');


second select you use 3 columns : 


$this->db->select('course_offrd_name,category_name,subcategory_name');


class Datacomplete extends CI_Model{

    public function GetRow($keyword) {        
       $this->db->select('collg_name,city,state,country as type');
       $this->db->from('tbl_college');
       $this->db->like("collg_name",$keyword);
       $this->db->or_like('city',$keyword,'after');
       $this->db->or_like('state',$keyword,'after');
       $this->db->or_like('country',$keyword,'after');
       $query1 = $this->db->get_compiled_select();
       $this->db->select('course_offrd_name,category_name,subcategory_name, null as type');
       $this->db->from('tbl_course_offered');
       $this->db->like("course_offrd_name",$keyword);
       $this->db->or_like('category_name',$keyword,'after');
       $this->db->or_like('subcategory_name',$keyword,'after');
       $query2 = $this->db->get_compiled_select();
       $result = $this->db->query($query1." UNION ".$query2);
       return $result->result();
    }
}

'$result = $this->db->query($query1." UNION ".$query2);' 您正在組合 2 個表格結果,但在第一個表格中您選擇了4列,而在第二個表格中您選擇了 3 列。

當您使用UNION組合時,它將導致記錄,這里兩個表列都不同,列數也不同。 要使用 UNION,您必須選擇相同的列數,如果可能,則只選擇相同的列。

在上述情況下發生錯誤 bcz:

                     + collg_name + city + state + type +
---------------------+------------+------+-------+------+
1st table record     + A          + b    + c     + d    +
---------------------+------------+------+-------+------+
2nd table record     + x          + y    + z     + -    +

第二個表沒有最后一列值

聯合在以下條件下執行:

SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;

結果 :

        + City +
1st tbl | city1|
1st tbl | city2|
2st tbl | city3|

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM