Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在codeigniter中加入三个sql表后,找不到所需的id 找不到所需的Id_Php_Mysql_Codeigniter_Join - Fatal编程技术网

Php 在codeigniter中加入三个sql表后,找不到所需的id 找不到所需的Id

Php 在codeigniter中加入三个sql表后,找不到所需的id 找不到所需的Id,php,mysql,codeigniter,join,Php,Mysql,Codeigniter,Join,我在CodeIgniter中加入了三个SQL表。我可以从三个表中检索数据。但是我在寻找正确的身份证时遇到了一个问题 我把我的三张桌子放在下面: 图书目录: 类别表: 用户表: 我的模型: 现在,我获得了联接表中用户的id。但是我希望books id作为主id。我如何解决这个问题?不要在查询中使用select*, 通过表名或表别名表示所需的列,因为有3个表已联接,并且都有一个id列,所以不知道要从查询结果中选择哪个id列 $this->db->select('books.id, boo

我在CodeIgniter中加入了三个SQL表。我可以从三个表中检索数据。但是我在寻找正确的身份证时遇到了一个问题

我把我的三张桌子放在下面:

图书目录: 类别表: 用户表: 我的模型: 现在,我获得了联接表中用户的id。但是我希望books id作为主id。我如何解决这个问题?

不要在查询中使用select*, 通过表名或表别名表示所需的列,因为有3个表已联接,并且都有一个id列,所以不知道要从查询结果中选择哪个id列

$this->db->select('books.id, books.status, users.someData, etc.');

下面,我只是从books表中选择所有列。我还将书籍作为主表,而不是类别表

public function get_books($limit, $offset)
{   
    /*=== SQL join ===*/
    $this->db->select('books.*');
    $this->db->from('books');
    $this->db->join('category', 'books.categoryId = category.id');
    $this->db->join('users', 'books.userId = users.id'); #...Join three sql table

    $this->db->order_by('books.id', 'DESC');
    $this->db->where('books.status', '1');
    $this->db->limit($limit, $offset);
    $query = $this->db->get();
    return $query->result();
}
如前所述,您不应在此处使用“*”。您应该手动编写select查询,从表中选择所需的任何列

要将书的id作为所需的id,您应该按如下所示更改模型代码

模型代码


我想现在您将从books表中获得所需的id。

您的查询将返回所有3个表中的所有列。没有主列。我想将books表作为我的主表。@ForPast这里没有主表或主列。只需删除*并选择所需的列。在两个表连接之前,此查询是正常的。@TahmidNishat我在回答中解释了您的问题。您正在连接多个具有id列的表,当您在设置的任何列上设置值时,它不知道您想要哪一列,因为您有3个从查询返回的列名为id的列,当然,当您仅从一个表中进行选择时,它会起作用,因为它是唯一具有id列的表。此查询无法从其他两个表中获取数据。它应该像$this->db->select'books.*,users.name,category.category;它解决了我的问题。这就是我想要的,谢谢@tumn
CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `name` varchar(200) NOT NULL,
  `contact` varchar(50) NOT NULL,
  `email` varchar(100) NOT NULL,
  `password` varchar(255) NOT NULL,
  `address` varchar(500) NOT NULL,
  `city` varchar(50) NOT NULL,
  `type` varchar(20) NOT NULL DEFAULT 'U',
  `createdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
public function get_books($limit, $offset)
{   
    /*=== SQL join ===*/
    $this->db->select('*');
    $this->db->from('category');
    $this->db->join('books', 'books.categoryId = category.id');
    $this->db->join('users', 'books.userId = users.id'); #...Join three sql table

    $this->db->order_by('books.id', 'DESC');
    $this->db->where('books.status', '1');
    $this->db->limit($limit, $offset);
    $query = $this->db->get();
    return $query->result();
}
$this->db->select('books.id, books.status, users.someData, etc.');
public function get_books($limit, $offset)
{   
    /*=== SQL join ===*/
    $this->db->select('books.*');
    $this->db->from('books');
    $this->db->join('category', 'books.categoryId = category.id');
    $this->db->join('users', 'books.userId = users.id'); #...Join three sql table

    $this->db->order_by('books.id', 'DESC');
    $this->db->where('books.status', '1');
    $this->db->limit($limit, $offset);
    $query = $this->db->get();
    return $query->result();
}
public function get_books($limit, $offset)
{   
    $this->db->select('books.id, books.book_name, books.description, books.author, books.book_image, books.otherCol, category.category, users.name'); // You can add many more if you want.

    $this->db->from('books');
    $this->db->join('category', 'books.categoryId = category.id'); //2nd join
    $this->db->join('users', 'books.userId = users.id'); //3rd join

    $this->db->order_by('books.id', 'DESC');
    $this->db->where('books.status', '1');
    $this->db->limit($limit, $offset);
    $query = $this->db->get();
    return $query->result();
}