【问题标题】:Error number: 1052. Column 'id' in field list is ambigious错误号:1052。字段列表中的列“id”不明确
【发布时间】:2016-12-09 10:42:08
【问题描述】:

我正在尝试使用 php 和 bootstrap 在 codeigniter 中创建一个分页表。至于我想在浏览器中运行我的代码,我面临标题中订阅的错误。如果有人可以帮助我,我将非常愉快和感激。这是我的代码。

employees_model.php

<?php
class employees_model extends CI_Model{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    //fetch department details from database
    function get_employees_list($limit, $start)
    {
        $sql = 'select (id, employee_emer, mbiemer, adresa, email), department_emer from employee, department where department.id = employee.id_departament order by employee_emer limit ' . $start . ', ' . $limit;
        $query = $this->db->query($sql);
        return $query->result();
    }
}
?>

employees.php

<?php
class employees extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->database();
        $this->load->library('pagination');

        //load the department_model
        $this->load->model('employees_model');
    }

    public function index()
    {
        //pagination settings
        $config['base_url'] = site_url('employees/index');
        $config['total_rows'] = $this->db->count_all('employee');
        $config['per_page'] = "5";
        $config["uri_segment"] = 3;
        $choice = $config["total_rows"] / $config["per_page"];
        $config["num_links"] = floor($choice);

        //config for bootstrap pagination class integration
        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_link'] = false;
        $config['last_link'] = false;
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['prev_link'] = '&laquo';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&raquo';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $this->pagination->initialize($config);
        $data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

        //call the model function to get the department data
        $data['emplist'] = $this->employees_model->get_employees_list($config["per_page"], $data['page']);           

        $data['pagination'] = $this->pagination->create_links();

        //load the department_view
        $this->load->view('employees_view',$data);
    }
}
?>

employees_view.php

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP CodeIgniter Pagination with Bootstrap Styles | Example</title>
    <!--link the bootstrap css file-->
    <link rel="stylesheet" href="<?php echo base_url("bootstrap/css/bootstrap.css"); ?>">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Employee ID</th>
                        <th>Employee Number</th>
                        <th>Employee Name</th>
                        <th>Employee Surname</th>
                        <th>Employee Address</th>
                        <th>Employee Email</th>
                    </tr>
                </thead>
                <tbody>
                    <?php for ($i = 0; $i < count($emplist); ++$i) { ?>
                    <tr>
                        <td><?php echo ($page+$i+1); ?></td>
                        <td><?php echo $emplist[$i]->id; ?></td>
                        <td><?php echo $emplist[$i]->employee_emer; ?></td>
                        <td><?php echo $emplist[$i]->mbiemer; ?></td>
                        <td><?php echo $emplist[$i]->adresa; ?></td>
                        <td><?php echo $emplist[$i]->email; ?></td>
                        <td><?php echo $emplist[$i]->department_emer; ?></td>
                    </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12 text-center">
            <?php echo $pagination; ?>
        </div>
    </div>
</div>
</body>
</html>

请帮助我。提前谢谢您!

【问题讨论】:

  • 我会从您的选择查询中删除这些括号。另外,请检查您的拼写,您似乎在您的联接中拼错了部门。最后一件事,开始使用正确的连接语法,你使用的是旧的和过时的查询风格。

标签: sql twitter-bootstrap codeigniter pagination


【解决方案1】:

尝试使用别名表名。检查这个

         $sql = ' select employee.id, employee_emer, mbiemer, adresa, email, department_emer from employee, department
          where department.id = employee.id_departament order by employee_emer limit ' . $start . ', ' . $limit;

或者

         $sql = ' select e.id, employee_emer, mbiemer, adresa, email, department_emer 
          from employee e inner join  department d on d.department.id = e.id_departament
          order by employee_emer limit ' . $start . ', ' . $limit;

【讨论】:

  • 错误号:1241 操作数应包含 1 列 select (employee.id, employee_emer, mbiemer, adresa, email), department_emer from employee, department where department.id = employee.id_departament order by employee_emer 限制 0, 5
  • 我尝试了两个不同的版本,我得到了与上面相同的错误
  • @Lilly 从 select 语句中取出那些括号
  • 一点建议,不要将变量直接连接到sql中。改用准备好的语句。 codeigniter 提供了一个不错的形式。
猜你喜欢
  • 2011-10-02
  • 2015-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-24
相关资源
最近更新 更多