How to write the given query in CodeIgniter - php

I want to write the given query in codeigniter , how can I do so?
SELECT * FROM `user` WHERE `id` IN (select `provider_id` from `services`);
I gone through codeigniter's where_in() but i'm getting wrong result , here what I have done,
$this->db->select('*');
$this->db->from(USER);
$this->db->where_in('id', 'select `provider_id` from `services`');
$query = $this->db->get();
return $query->num_rows();
it is returning 0, while the query above, when run directly in phpmyadmin, returns 1 record.

You can use this simple way $this->db->query('your query');
Please check this post, hope it will help you Using Mysql WHERE IN clause in codeigniter

You can pass direct queries in where class by passing second parameter as NULL and third parameter as FALSE
<?php
$where = 'id IN (select `provider_id` from `services`)';
$this->db->select('*');
$this->db->from(USER);
$this->db->where($where, NULL, FALSE);
$query = $this->db->get();
return $query->num_rows();

Use $this->db->query();
$query=$this->db->query("SELECT * FROM `user` WHERE `id` IN (select `provider_id` from `services`)");
$num=$query->num_rows();
$result= $query->result();

Related

how to pass variable value to sql query in codeigniter?

is it possible to pass the variable value in mysql query in codeigniter ?
I want to select records from database that have id same as my session 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();
An Example Reference of how to write the JOIN Query in CI as per the Documentation of the Active Records.
$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
In order to view the Result of the executed query you must perform the below code.
print_r($query->result());
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

what is going wrong in this query?

public function insrt_into_aprlog($id)
{
$query = 'insert into log_indent_apval (select id,indent_req_id,item_id,req_qty,approval_qty,approval_status,prod_categry,prod_type from tra_inent_rq_itm_dt where id ='.$id.')';
$result = $this->db->query($query);
$resultarr = $result->result_array();
}
my controller line:
$this->outpatient_model->insrt_into_aprlog($indent_id);
error : Call to a member function result_array() on a non-object
even i tried using in codeigniter model insert_batch(); still no use.
result_array() is used to generate query result . You are trying to insert data in your table i guess, so you shouldn't use that . Try below code.
public function insrt_into_aprlog($id)
{
$query = 'insert into log_indent_apval (select id,indent_req_id,item_id,req_qty,approval_qty,approval_status,prod_categry,prod_type from tra_inent_rq_itm_dt where id ='.$id.')';
return $this->db->query($query);
}
Please visit this
Reference
Look into your query.
INSERT INTO table1 ( column1, column2, someInt, someVarChar )
SELECT table2.column1, table2.column2, 8, 'some string etc.'
FROM table2
WHERE table2.ID = 7;
$query is being initialized to a string and then is accessed for an object. This is inconsistent for sure.
$query is a string, I guess you should use $result->result_array().

Escape the order by query in codeigniter

Here is the SQL query run :
SELECT * FROM (`news`) WHERE `country` IS NULL AND `region` IS NULL ORDER BY IFNULL(update_date, `create_date)` DESC
And you may notice that the create_date has some formatting error, I would like to disable the escape but even I add false after the order_by function it has no effect. How to fix it? Thanks a lot
$this->db->select('*');
$this->db->from('news');
$this->db->where($data);
$this->db->order_by('IFNULL(update_date,create_date)', 'DESC', false);
$query = $this->db->get();
return $query->result_array();
Use below code:
$this->db->_protect_identifiers = FALSE;
$this->db->order_by('IFNULL(update_date,create_date)', 'DESC', false);
$this->db->_protect_identifiers = TRUE;
add this line on top of your db select method.
$this->db->_protect_identifiers = FALSE;
Just to update, trying to make a sql select with multiplication to work on C.I. 3.10
$this->db->order_by("results.result * 1", 'ASC', FALSE);
Just adding , FALSE to order_by resolved because $this->db->_protect_identifiers was triggering an error on C.I. 3.10

codeigniter multiple OR query

I want to use following query in Codeigniter:
SELECT `users` . * , `users_profile` . * FROM (`users`) LEFT JOIN `users_profile`
ON `users`.`id` = `users_profile`.`users_id`
WHERE (`loginid` = '$username' OR `email` = '$username' )
AND `password` = '$password' AND `users`.`status` = 'A' LIMIT 1
It is working fine, but when I write this query in Codeigniter format:
$this->db->where('loginid',"$username");
$this->db->or_where('email', "$username");
$this->db->where('password',MD5($password));
$this->db->where('users.status','A');
$this->db->limit(1);
It will return always true what is right syntax?
May be your query should be like this in codeIgniter.......for reference you can see this link
Reference
$this->db->select('*');
$this->db->from('users');
$this->db->join('users_profile', 'users_profile.id = users.id', 'left');
$this->db->where("(loginid = '$username' OR email = '$username') AND password = 'MD5($password) AND status = 'A')'");
You should use the custom string method of where() as this is not possible directly with only ActiveRecord. See this link from the documentation and scroll to the 4th option under the where() function.
Example:
Custom string:
You can write your own clauses manually:
$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);
$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.
$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);

CodeIgniter: How To Do a Select (Distinct Fieldname) MySQL Query

I'm trying to retrieve a count of all unique values in a field.
Example SQL:
SELECT count(distinct accessid) FROM (`accesslog`) WHERE record = '123'
How can I do this kind of query inside of CodeIgniter?
I know I can use $this->db->query(), and write my own SQL query, but I have other requirements that I want to use $this->db->where() for. If I use ->query() though I have to write the whole query myself.
$record = '123';
$this->db->distinct();
$this->db->select('accessid');
$this->db->where('record', $record);
$query = $this->db->get('accesslog');
then
$query->num_rows();
should go a long way towards it.
try it out with the following code
function fun1()
{
$this->db->select('count(DISTINCT(accessid))');
$this->db->from('accesslog');
$this->db->where('record =','123');
$query=$this->db->get();
return $query->num_rows();
}
You can also run ->select('DISTINCT `field`', FALSE) and the second parameter tells CI not to escape the first argument.
With the second parameter as false, the output would be SELECT DISTINCT `field` instead of without the second parameter, SELECT `DISTINCT` `field`
Since the count is the intended final value, in your query pass
$this->db->distinct();
$this->db->select('accessid');
$this->db->where('record', $record);
$query = $this->db->get()->result_array();
return count($query);
The count the retuned value
Simple but usefull way:
$query = $this->db->distinct()->select('order_id')->get_where('tbl_order_details', array('seller_id' => $seller_id));
return $query;

Categories