i have some table, and the relationship goes like this
and i want to get all the record of those 2 tables. so i use this query in my model
$this->db->select('*');
$this->db->from('ms_Kategori_Material.*,ms_Material_Jasa.*');
$this->db->join('ms_Material_Jasa', 'ms_Kategori_Material.Kode_Kategori_Material_Jasa = ms_Material_Jasa.Kode_Kategori_Material_Jasa');
$result = $this->db->get();
$table = $this->db->get();
return $table;
and then i got error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '*, `ms_Material_Jasa`.*) JOIN `ms_Material_Jasa` ON `ms_Kategori_Material`.`Kode' at line 2
SELECT * FROM (`ms_Kategori_Material`.*, `ms_Material_Jasa`.*) JOIN `ms_Material_Jasa` ON `ms_Kategori_Material`.`Kode_Kategori_Material_Jasa` = `ms_Material_Jasa`.`Kode_Kategori_Material_Jasa`
why do i can't read the needed table ?
in your from, there shouldnt be a .*,
i.e
$this->db->from('ms_Kategori_Material','ms_Material_Jasa');
and if your adding the table to join, no need to add it to from.
so it becomes
$this->db->from('ms_Kategori_Material');
$this->db->join('ms_Material_Jasa','....');
final query:
$this->db->select('*');
$this->db->from('ms_Kategori_Material');
$this->db->join('ms_Material_Jasa', 'ms_Kategori_Material.Kode_Kategori_Material_Jasa = ms_Material_Jasa.Kode_Kategori_Material_Jasa');
$result = $this->db->get();
$table = $this->db->get();
return $table;
You may try this (to join and select fields from both tables)
$this->db->select('ms_Kategori_Material.*, ms_Material_Jasa.*');
$this->db->from('ms_Kategori_Material');
$this->db->from('ms_Material_Jasa');
$this->db->join('ms_Material_Jasa', 'ms_Kategori_Material.Kode_Kategori_Material_Jasa = ms_Material_Jasa.Kode_Kategori_Material_Jasa');
$table = $this->db->get();
return $table->result();
$this->db->select('*');
$this->db->from('ms_Kategori_Material'); // full table name
$this->db->join('ms_Material_Jasa', 'ms_Kategori_Material.Kode_Kategori_Material_Jasa = ms_Material_Jasa.Kode_Kategori_Material_Jasa');
$result = $this->db->get();
//print_r($this->db->last_query()); display raw sql
$print_r($result->result_array());
Read more # http://codeigniter.com/user_guide/database/active_record.html
Related
so i kinda new with codeigniter and im trying to join 3 table from my database
database 1 : dkm (id, tgl, ref, etc)
database 2 : order_product (kode_barang, packing, nama_barang, etc)
database 3 : product (kodeprod, tglpakai, etc)
im already trying what other ppl do to join more than 2 table in codeigniter but i got this error :
Error Number: 1066
Not unique table/alias: 'order_product'
SELECT *
FROM `order_product`
JOIN `order_product` ON `order_product`.`kode_barang` = `dkm`.`id`
JOIN `order_product` ON `order_product`.`kode_barang` = `produksi`.`kodeprod`
This is my code :
Bukaka_model.php
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('order_product','order_product.kode_barang = dkm.id');
$this->db->join('order_product','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
return $query->result();
}
You're trying to join to the same table multiple times, instead you need to join to the other tables once each.
You just need to change the names of the table you're joining to:
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','order_product.kode_barang = dkm.id');
$this->db->join('produksi','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
return $query->result();
}
Try this,
Here, you have a mistake in joining tables, in CI join() in the first parameter you need to pass/write table name with you want to join
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','order_product.kode_barang = dkm.id');
$this->db->join('produksi','order_product.kode_barang = produksi.kodeprod');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}else{
return array();
}
}
Try this:
public function getOrderProduct()
{
$this->db->select('*');
$this->db->from('order_product');
$this->db->join('dkm','dkm.id= order_product.kode_barang');
$this->db->join('produksi',' produksi.kodeprod = order_product.kode_barang');
$query = $this->db->get();
return $query->result();
}
code:
public function draft_post($idd)
{
$this->db->select('*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id','INNER');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
In this codes, I have two table i.e. registration and draft_registration. Now, What am I doing here I want to run inner join in Codeigniter. Now, What happening when I hit this query on phpmyadmin it shows wrong data i.e. I have two rows in draft_registration and one row in registration table but it always shows two table which is wrong and my query looks like when I was print as mention below:
SELECT *
FROM `registration`
INNER JOIN `draft_registration` ON `registration`.`user_id`= `draft_registration`.`user_id`
WHERE `registration`.`user_id` = '20181121064044'
So, How can I resolve this issue? Please help me.
Thank You
$this->db->select('*'); //This code get all rows from both table.If you want a particular row you mention the column name.
For example:
$this->db->select('registration.name,draft_registration.city,draft_registration.state');
Specify column that you want to select. Or if you want select all column of your table, you can use :
SELECT registration.* with backticks `` on column name
Use the Below Code
public function draft_post($idd)
{
$this->db->select('registration.*,draft_registration.*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
Or you can use with objects
public function draft_post($idd)
{
$this->db->select('a.*,b.*');
$this->db->from('registration a');
$this->db->join('draft_registration b', 'a.user_id= b.user_id');
$this->db->where('a.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
My model:
public function getSolServicoById($id){
$select = 'SELECT * FROM solicitacao_servico WHERE id_solicitacao = "$id" LIMIT 1';
$query = $this->db->query($select);
return $query->result();
}
My controller:
public function editaSolicitacao($id){
$this->load->model('Pedido_Model','pedido');
echo $id;
$data = $this->pedido->getSolServicoById($id);
print_r($data);
}
When i select it on database i receive rows but when i select in application i get empty array and i don't know why it happen?!
Try this :
$select = "SELECT * FROM solicitacao_servico WHERE id_solicitacao = '{$id}' LIMIT 1";
Also look forward to using prepared statements to reduce sql-injection vulnerability.
A better way to do this, just because its a simple query in CI is:
$this->db
->select('*')
->from('solicitacao_servico')
->where('id_solicitacao',$id)
->limit(1)
->get();
Doing it this way doesn't constrain your code to a particular database type (MySQL, MSSQL, etc) because it will create the correct syntax for your application with the built in active record feature.
I'am using CodeIgniter active records and I have a lack in knowledge of Joining multiple table columns in MySQL.
What I basically want to do, is to output all rows from a ci_categories table, but instead of showing ci_categories.component_id as a numeric, i wish to output the ci_components.name which have equal (or same) ci_components.id from another table ci_components by using the Active Records in CodeIgniter.
What I have already done, but with errors is:
Model:
public function getItemName($id){
$this->db->select('*');
$this->db->where('id', $id);
$result = $query->result();
return $result;
}
and View:
<?php echo $this->component_model->getItemName($cat['category']->com_id);?>
Any tips pls ?
$this->db->select('name')
->from('ci_components')
->where('ci_categories.component_id = ci_componenents.id');
$query = $this->db->get();
or
$this->db->select('name');
$this->db->from('ci_components');
$this->db->join('ci_categories', 'categories.component_id = ci_components.id');
$query = $this->db->get();
https://www.codeigniter.com/userguide2/database/active_record.html#select
I have 3 tables in my database :-
tbl_roles(role_id,role_name);
tbl_users(id,role_id,username,email,password);
tbl_tickets_replies(id,ticket_id,user_id,role_id,comments)
role_id, id, id are primary keys of corresponding tables.
i need :-
username from tbl_users.
role_name from tbl_roles.
comments from tbl_tickets
where ticket_id from tbl_tickets_replies = $ticket_id coming as a parameter.
My Model Function is :-
function fetch_comments($ticket_id){
$this->db->select('tbl_tickets_replies.comments,tbl_users.username,tbl_roles.role_name');
$this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
$this->db->from('tbl_tickets_replies');
$this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
$this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
$comments = $this->db->get('tbl_tickets_replies');
return $comments;
}
this is showing database error i.e., I am doing wrong query.
I want to ask how can I join three tables to fetch data from 3 different tables
This error is showing :-
A Database Error Occurred
Error Number: 1066
Not unique table/alias: 'tbl_tickets_replies'
SELECT tbl_tickets_replies.comments, tbl_users.username,
tbl_roles.role_name FROM (tbl_tickets_replies,
tbl_tickets_replies) JOIN tbl_users ON tbl_users.id =
tbl_tickets_replies.user_id JOIN tbl_roles ON
tbl_roles.role_id=tbl_tickets_replies.role_id WHERE
tbl_tickets_replies.ticket_id = '6'
Filename: C:\wamp\www\local.helpdesk.com\bonfire\codeigniter\database\DB_driver.php
Line Number: 330`
You are referring to tbl_tickets_replies twice.
Try this:
function fetch_comments($ticket_id){
$this->db->select('tbl_tickets_replies.comments,
tbl_users.username,tbl_roles.role_name');
$this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
$this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
$this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
return $this->db->get('tbl_tickets_replies');
}
For complex queries, i prefer using the plain SQL as follows.
$sql = "SELECT....";
$q = $this->db->query($sql);
Btw, try removing the table name from db->get function
$comments = $this->db->get(); //change this
Join with condition.
$this->db->select('*'); $this->db->from('articles');
$this->db->join('category', 'category.id = articles.id');
$this->db->where(array('category.id' => 10)); $query =
$this->db->get();