错误号: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
我该如何解决这个问题?
此外,如果问题解决了,我的搜索是否将从数据库中获取数据。 代码有什么问题?
答案 0 :(得分:1)
您必须在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();
}
}
答案 1 :(得分:0)
在'$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|