[英]how to pass variable value to sql query in codeigniter?
is it possible to pass the variable value in mysql query in codeigniter ? 是否有可能在codeigniter中的mysql查询中传递变量值?
I want to select records from database that have id same as my session id. 我想从数据库中选择与我的会话ID相同的记录。
my code is : 我的代码是:
$this->db->select('*'); // <-- There is never any reason to write this line!
$this->db->from('products');
$this->db->where('id_admin = (SELECT id_admin FROM users WHERE id='.$this->session->userdata('user_id').')', NULL, TRUE);
can we concatenate variable values in database queries? 可以在数据库查询中连接变量值吗?
This code not given any error, but not give any result also. 此代码未给出任何错误,但也未给出任何结果。
Use join in active records 在活动记录中使用联接
$this->db->select('p.*');
$this->db->from('products as p');
$this->db->join('users','p.id_admin=users.id');
$this->db->where('users.id',$this->session->userdata('user_id'),false);
$query = $this->db->get();
Try like this..use joining of two tables. 尝试这样。使用两个表的联接。
<?php
$this->db->select('products.*');
$this->db->from('products');
$this->db->join('users','products.id_admin=users.id_admin');
$this->db->where('users.id',$this->session->userdata('user_id'));
$query = $this->db->get();
print_r($query->result_array());//your array of records
An Example Reference of how to write the JOIN Query in CI as per the Documentation of the Active Records. 根据活动记录的文档,如何在CI中编写JOIN查询的示例参考。
$article_id = $this->input->post('article_id');
$this->db->select('*');
$this->db->from('articles');
$this->db->join('news', 'news.id = articles.id');
$this->db->where('articles.id',$article_id);
$query = $this->db->get();
The above code will produce the query as follows for execution. 上面的代码将按如下所示生成查询以执行。
SELECT * FROM articles JOIN news ON news.id = articles.id WHERE articles.id='10'
Provided the passed id is 10 如果传递的ID为10
In order to view the Result of the executed query you must perform the below code. 为了查看已执行查询的结果,您必须执行以下代码。
print_r($query->result());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.