I'm using Codeigniter active records for some time now and this is the most ridiculous error I've got.
My query is
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
//return $query->result();
return $query;
} else {
return FALSE;
}
when I use$query->result(); It is empty. When I use return $query;
the result is like below,
CI_DB_pdo_result Object
(
[num_rows] => 21
[conn_id] => PDO Object
(
)
[result_id] => PDOStatement Object
(
[queryString] => SELECT * FROM my_table WHERE is_active = 1
)
[result_array] => Array
(
)
[result_object] => Array
(
)
[custom_result_object] => Array
(
)
[current_row] => 0
[row_data] =>
)
Count is
[num_rows] => 21
what is missing/problem here?
I have found the issue. This thing happens because my application use PDO DB Driver. Not MySQL Driver.
$db['default']['dbdriver'] = 'pdo';
So I changed my model to support that.
$sql = "SELECT * FROM my_table WHERE is_active = 1";
$stmt1 = $this->db->conn_id->prepare($sql);
$stmt1->execute();
return $stmt1->fetchAll(PDO::FETCH_ASSOC);
Now it's working fine.
I am really sorry for your time people. And thank you very much for
trying to help.
Try this
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get();
$result = $query->result_array();
if (count($result) > 0) {
return $result;
}
else{
return FALSE;
}
You're not formatting the query array. So format it.
simple add
$query = $this->db->get()->result();
in your code
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get()->result();
if ($query->num_rows() > 0) {
//return $query->result();
return $query;
} else {
return FALSE;
}
if ($query->num_rows() > 0) then return $query->result() which returns results in object format.Like this..
if ($query->num_rows() > 0) {
return $query->result();
} else {
return FALSE;
}
Then use arrow(->) operator to retrieved required values.
For more see here Codeigniter Result Formats
If u want to get result you must use row() or result() function.
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
//return $query->result();
return $this->db->get()->result();
//return $query;
} else {
return FALSE;
}
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get();
$i = 0;
foreach ($query->result() as $row) {
$i++;
$data['id'] = $row->id;
$data['username'] = $row->username;
}
if ($i > 0) {
return $data;
}
Related
i want to make pagination on ci from other web reference, but average of them use only 1 table
does anyone know how to input more than 1 table on this condition
public function data($number,$offset){
return $query = $this->db->get('client',$number,$offset)->result();
}
i have try to input like this
public function data($number,$offset){
return $query = $this->db->get('client,advertisement,size',$number,$offset)->result();
}
but the result is not my expected
the NAMA is looping, the IKLAN too but the SIZE is not
i take the refences from here
I use this:
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$this->db->join('client', 'advertisement.id_client= client.id_client','inner');
$this->db->join('size', 'advertisement.id_size= size.id_size','inner');
$query = $this->db->get("advertisement");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Using Codeigniter 3, I would like to display all the records from a table in a MySQL database. I'd also like to include the number of records selected.
For example;
Showing x number of records;
record 1
record 2
record 3
etc
Currently I have the following (which works);
// select all records
public function selectRecords() {
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
return $query->result_array();
}
// count all records
public function countRecords() {
$this->db->select('count(*) as count');
$this->db->from('records');
$query = $this->db->get();
return $query->row();
}
My question is do I need two separate queries in order to achieve this (select and count)?
Is there a more efficient way of achieving what I want?
You can do something like this :
public function selectRecords()
{
$query = $this->db->get('records');
if ($query->num_rows() > 0 )
{
$records = $query->result_array();
$data['count'] = count($records);
$data['all_records'] = $records;
return $data;
}
}
Pass it to the view from your controller :
$data = $this->model_name->selectRecords();
/*print_r($data) to see the output*/
$this->load->view('your_view',$data);
In view :
<?php echo $count .' number of records';?>
you can do only:
public function selectRecords() {
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
return $query->result_array();
}
and
$records = $this->selectRecords();
$count = count($records);
In The first function itself you can get the count using $query->num_rows() function
public function selectRecords() {
$return = array();
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
$return['count'] = $query->num_rows();
$return['records'] = $query->result_array();
return $return;
}
try this
it will help you to provide pagination for records
public function selectRecords($params = array(), $count = false) {
$offset = isset($params['offset']) ? $params['offset'] : '';
$limit = isset($params['limit']) ? $params['limit'] : '';
$this->db->select('*');
$this->db->from('records');
$query = $this->db->get();
if ($count) {
return $this->db->get()->num_rows();
}
if (empty($offset) && !empty($limit)) {
$this->db->limit($limit);
}
if (!empty($offset) && !empty($limit)) {
$this->db->limit($limit, $offset);
}
$result = $this->db->get()->result();
return $result;
}
I am getting this Error Number: 1096 No tables used SELECT * ,my code is below not getting what is wrong in that
public function product_approve($id) {
$this->db->select('*');
$this->db->from('approve_products');
$this->db->where('id',$id);
$query= $this->db->get();
if ( $this->db->get()->num_rows() > 0 )
{
foreach ($query->result() as $row) {
$this->db->insert('sub3_category',$row);
}
// $this->db->insert('sub3_category',$query);
if($this->db->affected_rows() >= 0){
$this->db->where('id', $id);
$this->db->delete('approve_products');
}
}
}
Change following line of your code
if ( $this->db->get()->num_rows() > 0 )
with
if ( $query->num_rows() > 0 )
Hope this helps.
Change
$query= $this->db->get();
if ( $this->db->get()->num_rows() > 0 )
Into
$query= $this->db->get('approve_products');
if( $query->num_rows() > 0 )
And also remove/comment the following line.
$this->db->from('approve_products');
Ref
You can use $this->db->from('approve_products') too but not $this->db->from('approve_products') and $this->db->get('approve_products') both at same.
private function _get_datatables_queryarc($id,$table)
{
if ($id==1){
$this->db->select('`id`,`name`,`gov`,`proince`');
$this->db->from($table);
}
So I am pulling 10 records from database at the time using AJAX call.
below is my code:
public function get_next_10($offset = 0)
{
$this->db->limit(15, $offset);
$query = $this->db->get("postovi");
return $query->num_rows() > 0 ? $query->result_array() : NULL;
}
I tried putting:
$this->db->order_by("name", "asc");
but it's throwing an error.
Its Working Fine
public function get_next_10($offset = 0)
{
$this->db->limit(15, $offset);
$this->db->order_by('name', 'asc');
$query = $this->db->get("postovi");
return $query->num_rows() > 0 ? $query->result_array() : NULL;
}
I am working in codeigniter.I have created one function in model.The function is like this :
function pr($id)
{
//$query5 = $this->db->query("select max(to_date) as last_date from agent_print_details where agent_id = ".$id);
//$result5 = $query5->result();
$this->db->select('max(to_date) as last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id',$id);
$result = $this->db->get();
if($result->num_rows()>0)
return $result->result();
else
return "empty";
}
my controller is :
$pr_detail = $this->cashier1_model->pr($role['id']);
if($pr_detail != 0)
{
echo "nisarg";
}
else
{
echo "123";
}
when I print pr_detail then it will display output like this:
Array ( [0] => stdClass Object ( [last_date] => ) )
it will give blank data so it has to print 123 but it is display nisarg.
So What should i have to do to print 123?
If result not found just return FALSE in model file. Also use CI select_max to get max value
MODEL
$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
if ($result->num_rows() > 0) {
return $result->result();
} else {
return FALSE;
}
CONTROLLER
$pr_detail = $this->cashier1_model->pr($role['id']);
if($pr_detail)
{
echo "nisarg";
}
else
{
echo "123";
}
you could use count function, also, do you have defined a table name in get function?
...
$result = $this->db->get('agent_print_details');
//if($result->num_rows() > 0)
if(count($result) > 0)
...
When you use $result->num_rows() > 0 you CANT use $result->result(). If u want to return these results, you need to check if its not empty first, and then query again to get results.
$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
if ($result->num_rows() > 0) {
$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
return $result->result();
} else {
return FALSE;
}