简体   繁体   中英

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

Error Number: 1222
The used SELECT statements have a different number of columns

This is controller

    <?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);
        }

    }
    ?>

This is model

        <?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();
        }
    }

This is view

    <!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());
        });
    });

Here im trying to search a keyword from three table using codeigniter and ajax.
I'm getting that error in the model

How can I solve this issue?

Also, if the issue is solved whether my search will fetch the data from database. What is wrong in the code?

You have to use same column number in your select statement as following

First select you use 4 columns :

$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();
    }
}

In '$result = $this->db->query($query1." UNION ".$query2);' you are combining 2 table results but in 1st table you are selecting 4 columns & in 2nd table you are selecting 3 columns.

When You combine using UNION then it will result into records & here both table columns are different and also the column count is different. To use UNION you have to select same column count & if possible then select same columns only.

In above situation error occure bcz:

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

2nd table don`t have last column value

Union execute in below condition:

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

Result :

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

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