i want to write a query like select*from bookdetails where editionId=$edid1 or editionId=$edid2; in Codeigniter query.
Below is my Codeigniter query
public function get_rare_outofprintBooks($edid1,$edid2)
{
$this->load->database();
$query = $this->db->get_where('bookdetails',array('edition_id'=>$edid1),array('edition_id'=>$edid2)); //i tried like this but its NOT Working
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
}
Please help me to solve this issue.
$this->db->where('editionId', $edid1);
$this->db->or_where('editionId', $edid2);
It's right in the documentation http://ellislab.com/codeigniter/user-guide/database/active_record.html
$this->db->where('id',3);
$this->db->or_where('id',5);
Reference Here
Related
I am fetching data from mysql database table in codeigniter using following code
$result = $this->db->get("shipping");
$data = $result->result_array();
But returns no data.
When I apply the limit as
$this->db->get("shipping",1,30)
The code works.
But I want to fetch all result and not by limiting it.
Could anyone please let me know how do I resolve this issue.
Thanks in advance.
have you tried using query rather than get?
$sql = "
SELECT
*
FROM
shipping
";
$query = $this->db->query($sql);
$data = $query->result();
You have not put a return
https://www.codeigniter.com/user_guide/database/results.html
// This will get every result from shipping
public function somefunction() {
$result = $this->db->get("shipping");
return $result->result_array();
}
Controller function
public function somecontrollerfunction() {
$this->load->model('some_model');
$data['shipping'] = array();
$data['shipping'] = $this->some_model->somefunction();
// var_dump($data['shipping']);
$this->load->view('your_view', $data);
}
I m using codeigniter with MSSQL db. to get a number of records from a query I m using $query->num_rows(). but its not working. If I use MYSQL DB then its working fine. My code is -
function getCountry(){
$this->db->distinct();
$this->db->select('CM_CHANNEL_TELCAS_DETAIL.CTD_CNTRY_CODE as CNTRY_CODE, CM_COUNTRY.CC_CNTRY_NAME as CNTRY_NAME');
$this->db->from('CM_CHANNEL_TELCAS_DETAIL');
$this->db->join('CM_COUNTRY','CM_CHANNEL_TELCAS_DETAIL.CTD_CNTRY_CODE = CM_COUNTRY.CC_CNTRY_CODE','inner');
$query = $this->db->get();
if($query -> num_rows() > 0){
return $query->result();
}else{
return false;
}
}
If I write above code like this then It gives the results -
function getCountry(){
$this->db->distinct();
$this->db->select('CM_CHANNEL_TELCAS_DETAIL.CTD_CNTRY_CODE as CNTRY_CODE, CM_COUNTRY.CC_CNTRY_NAME as CNTRY_NAME');
$this->db->from('CM_CHANNEL_TELCAS_DETAIL');
$this->db->join('CM_COUNTRY','CM_CHANNEL_TELCAS_DETAIL.CTD_CNTRY_CODE = CM_COUNTRY.CC_CNTRY_CODE','inner');
$query = $this->db->get();
return $query->result();
}
I tried to echo $query->num_rows() value. but its not giving any value.
Please help me to resolve this issue.
Thanks in Advance.
Try with other method $this->db->count_all_results()
if($this->db->count_all_results() >0)
{
return $query->result();
}
else
{
return false;
}
try this
echo $query->affected_rows();
hope help youu
I have a model which fetch the data from database is below
public function counselor() {
$inst_id = $this->session->userdata('user_id');
$submission_key=$this->session->userdata('submission_key');
$query = $this->db->query("SELECT * FROM counselor where USER_ID = $inst_id AND submission_key= $submission_key");
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}
}
I have tested the $inst_id and $submission_key by printing it and its set.
$inst_id=2 and $submission_key=2016-8 .BUT though I have one record in database with those two field set its not returning the data. What is the case. I have tried with codeigniter get() and where() method too. Still not giving me the result.
Just write your query using active record function. It will help you in escaping string
$this->db->select('*',FALSE);
$this->db->where('USER_ID',$inst_id);
$this->db->where('submission_key',$submission_key);
$query=$this->db->get('counselor');
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}
I have finally got Codeigniter to work with ignited datatables. Now i have run into a different problem. Can anyone help or tell me if i could run the below query with the datatables plugin for codeigniter.
At present i'm doing it within the controller which is lame i know (this was only for testing)
Controller
$data['query'] = $this->test_queries->list_partners();
foreach($data['query'] as $k => $company){
$data['query'][$k]->partner_contacts = $this->test_queries->get_partner_contacts($company->id);
}
Queries in the Model
function list_partners(){
$this->db->select("company.id,name,general_email,general_phone,market");
$this->db->from("company");
$this->db->join('markets','markets.id = company.market_id');
$query = $this->db->get();
$result = $query->result();
return $result;
}
function get_partner_contacts($id){
$this->db->select('partner_contacts.id,contact_type');
$this->db->from('partner_contacts');
$this->db->where('company_id',$id);
$this->db->join('department','department.id = partner_contacts.contact_type_id');
$query = $this->db->get();
$result = $query->result();
return $result;
}
You can change the queries in the model as below:
function list_partners(){
$this->datatables->select("company.id,name,general_email,general_phone,market");
$this->datatables->from("company");
$this->datatables->join('markets','markets.id = company.market_id');
return $this->datatables->generate();
}
function get_partner_contacts($id){
$this->datatables->select('partner_contacts.id,contact_type');
$this->datatables->from('partner_contacts');
$this->datatables->where('company_id',$id);
$this->datatables->join('department','department.id = partner_contacts.contact_type_id');
return $this->datatables->generate();
}
You can also use method chaining if you are using PHP 5 or above.
unable to use order_by random in codeigniter
My Code is:
function fetch_popular_news()
{
$query=$this->db->order_by('id','rand')->limit(1)->get('main_slider');
$data=$query->result();
return $data;
}
i also tried:
function fetch_popular_news()
{
$query=$this->db->order_by('id','RANDOM')->limit(1)->get('main_slider');
$data=$query->result();
return $data;
}
but it's not working...
Use this instead, it will work:
order_by('rand()')
In codeignitor you need to specify it as follows
function fetch_popular_news()
{
$this->db->order_by('id', 'RANDOM');
$this->db->limit(1);
$query = $this->db->get('main_slider');
return $query->result_array();
}
Another way would be
function fetch_popular_news()
{
$query = $this->db->query('SELECT * FROM main_slider ORDER BY RAND() LIMIT 1');
return $query->result_array();
}
Friends I have found the solution
$query=$this->db->order_by('rand()')->limit(1)->get('main_slider');
anyway thanks to every one for help me...