Getting compiled WHERE statement from Codeigniter db class - php

Currently working on a small project with a few different database functions. I am trying to figure out a way to get the compiled Codeigniter WHERE statement.
// Database GET function
function dbGetRow($id, $field)
{
$this->db->select($field)->from('friends');
$query = $this->db->get();
return $query->row_array();
}
// Calling the function
$this->db->where('id', 2);
$value = parent::dbGetRow(null 'id');
What i am trying to figure out is how to get the compiled WHERE statement inside of the dbGetRow() function before performing the query.

Got this working.
the only solution is to use $this->db->get_compiled_select();
private function hasInlineQuery()
{
$string = $this->db->get_compiled_select();
$string = str_ireplace('SELECT *', '', $string);
return $string ? true : false;
}

Related

codeigniter search function is not working

I want to create simple search function. I follow some example. but I was unable to get results. please help me.
//control page
public function search(){
$key = $this->input->post('phone1',TRUE);
$data['ppp'] = $this->admin_model->searching($key);
$this->load->view('members/search_result',$data);
}
//Model page
public function searching($key){
$this->db->like('phone1',$key);
$query = $this->db->get('advertisement');
return $query->result();
}
try to print query
$query = $this->db->get('advertisement');
echo $this->db->last_query();exit;
try and check that query in your database is that giving result or not

How to call multiple store procedure in controller codeigniter?

I have a problem when I want to call more than one stored procesure in the controller. just only one stored procedure that execute.
This query stored procedure in the model :
function getKategori(){
$query = $this->db->query("call KategoriSelectPro('id_kategori','kategori')");
return $query->result();
}
function getEditSubKategori($id_subkategori){
$query = $this->db->query("call SubKategoriEditSelectPro(?,'id_kategori','kategori','sub_kategori')", $id_subkategori);
return $query->row_array();
}
and this code in controller:
function subkategoriedit($id_subkategori = ''){
$data['kategori'] = $this->madmin->getKategori();
$data['editsubkategori'] = $this->madmin->getEditSubKategori($id_subkategori);
}
The problem is only one function of the model can be called in the controller. example:
$data['kategori'] = $this->madmin->getKategori(); (SUCCESS)
$data['editsubkategori'] = $this->madmin->getEditSubKategori($id_subkategori); (NOT RUN)
The error message is :
Commands out of sync; you can't run this command now
so if reversed.
help me, how to call multiple store procedure in CodeIgniter?
I had the same problem a couple of moments earlier, searched for it on stackoverflow, found the answer and forgot to rate the answer .. anyhow
add the following function in file
system > database > drivers > mysqli > mysqli_driver.php
function next_result()
{
if (is_object($this->conn_id))
{
return mysqli_next_result($this->conn_id);
}
}
and then add the following command after every calling query you perform.
$this->db->next_result();
it worked like a charm for me ..
in your model change return $query->row_array(); to return $query->result_array();
function getKategori(){
$query = $this->db->query("call KategoriSelectPro('id_kategori','kategori')");
return $query->result();
}
function getEditSubKategori($id_subkategori){
$query = $this->db->query("call SubKategoriEditSelectPro(?,'id_kategori','kategori','sub_kategori')", $id_subkategori);
return $query->result_array();
}

Codeigniter php MVC

I have a table called 'News' with three columns: 'id', 'title' and 'details'
I have a function ('get_entry') inside a codeigniter model class (called 'News_model')
function get_entry()
{
$this->load->database();
return $this->db->select('id,title,details')->from ('news');
$data['newsarray'] = $this->db->row_array();
return $data['newsarray'];
}
I am connecting to the db so that is not the problem.I want to return an iterable array from get_entry() by calling the function from a controller file with the followlwing code. I want to push it into another array (called '$data['theNews']') using the code below.
foreach ($this->News_model->get_entry() as $key => $value){
array_push($data['theNews'],$value->title);
}
I have been using the code on this (https://www.codeigniter.com/user_guide/general/models.html) as a template (in particular the function 'get_last_ten_entries()' but I think I am close with the code I posted above. I would appreciate any help.
About your code:
You have two 'return' in your get_entry function:
function get_entry()
{
$this->load->database();
// First
return $this->db->select('id,title,details')->from ('news');
$data['newsarray'] = $this->db->row_array();
// Second
return $data['newsarray'];
}
Change it to:
function get_entry()
{
$this->load->database();
$query = $this->db->select('id,title,details')->from('news');
$data['newsarray'] = $query->row_array();
return $data['newsarray'];
}
It should work now.
Some advices:
Don't use Codeigniter 2 anymore. Version 3 is alive.
If you plan to return whole table columns, i suggest you to use the following code for the query:
$query = $this->db->get('news', 1, 20);
Where 1, 20 is the limit.
Now you can get the result:
return $query->result();
A simple example:
function get_entry()
{
$this->load->database();
$query = $this->db->get('news', 1, 20);
return $query->result();
}
This method returns the query result as an array of objects that you can print like so in your controller:
$news_array = $this->News_model->get_entry();
foreach ($news_array as $news)
{
echo $news->id;
}
Look at CI 3 Query Builder query builder for more examples.
One more suggestion, just autoload the database library in application/config/autoload.php if you need it globally.
Changing the code to this in the function worked:
function get_entry()
{
$this->load->database();
$query = $this->db->get('news');
//return $query->result();
foreach ($query->result() as $row)
{
echo "</br>";
echo $row->id;
echo "</br>";
echo $row->title;
echo "</br>";
echo $row->details;
echo "</br>";
}
}
Calling the function like so prints it out:
$news_array = $this->News_model->get_entry();

Codeigniter with Ignited Datatables

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.

Codeigniter Extracting Data From Database With Function

I have to extract two separate pieces of information from my mysql database. I'm having a hard time trying to figure out how to extract two different sets of information via function(s) I'm writing. I'm trying to figure out a solution but I'm not getting it. Below is my syntax so far. My goal here is to get both of the functions (getPrice and getOtherPrice) all within the same function working. If I // one, the other one works. If both are active, just one is working. How would you guys go about correcting this issue, what do you think I'm doing wrong? Thanks Everyone.
function getJoinInformation($year,$make,$model)
{
$data = $this->getPrice($year,$make,$model);
$data = $this->getOtherPrice($year,$make,$model);
return $data;
}
function getPrice($year,$make,$model)
{
$this->db->select('*');
$this->db->from('tbl_car_description d');
$this->db->join('tbl_car_prices p', 'd.id = p.cardescription_id');
$this->db->where('d.year', $year);
$this->db->where('d.make', $make);
$this->db->where('d.model', $model);
$query = $this->db->get();
return $query->result();
}
function getOtherPrice($year,$make,$model)
{
$this->db->select('*');
$this->db->from('tbl_car_description d');
$this->db->where('d.year', $year);
$this->db->where('d.make', $make);
$this->db->where('d.model', $model);
$query = $this->db->get();
return $query->result();
}
you are returning only last function result you should put the the result of both function by making an array of $data
function getJoinInformation($year,$make,$model)
{
$data['getPrice'] = $this->getPrice($year,$make,$model);
$data['getOtherPrice'] = $this->getOtherPrice($year,$make,$model);
return $data;
}
You're replacing the result in the variable $data.
$data = $this->getPrice($year,$make,$model);
$data = $this->getOtherPrice($year,$make,$odel);
$data will always just contain the result of the last function.

Categories