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
Related
Error at num_rows() as undefined member function. For dispalying data in descending order in codeigniter Framework.
I tried this code:
$this->db->select('*');
$this->db->from('albums');
$result = $this->db->order_by('album_id', 'desc');
//$result = $this->db->get('albums');
if($result->num_rows() > 0)
{
return $result;
}
For fetching and displaying rows in descending order i used the above code. But i am getting the following error at num_rows() as undefined function. How do I print it in descending order?
Change you'r code to :
$this->db->select('*');
$this->db->from('albums');
$result = $this->db->order_by('album_id', 'desc');
$result = $this->db->get('albums')->row();
if($result->num_rows() > 0)
{
return $result;
}
You will need to execute get() before getting the count of rows.
So your Code should look like this,
$this->db->select('*');
$this->db->from('albums');
$this->db->order_by('album_id', 'desc');
$result = $this->db->get(); // add this line
if($result->num_rows() > 0)
{
return $result;
}
get() function runs the selection query.
Use this code
$this ->db->select('*');
$this ->db->from('albums');
$this->db->order_by('album_id', 'desc');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}
If you need help hit me up :)
EDIT
You need
$query->result();
To return it as an object, but you you like you can use
$query->result_array();
To return it as array, don't just return $query as it is :)
I cant figure out whats wrong with my model, it said a Fatal Error occured.
Here's my model file:
function getName($no_ktp){
$this->db->select('nama')->from('dt_prbd')->where('no_ktp', $no_ktp);
$qry_getName = $this->db->get();
if ($qry_getName->num_rows() > 0) {
foreach ($qry_getName->result() as $data_getName){
$hasil_qry_getName[] = $data_getName;
}
return $hasil_qry_getName;
}
}
I got this error.
Fatal error: Call to a member function num_rows() on boolean in C:\xampp\[APP_PATH]\M_hrd_apps.php on line 25
I thought the error is in the query, so I changed it into this:
$qry_getName =
$this->db->select('nama')
->from('dt_prbd')
->where('no_ktp', $no_ktp)
->get();
but the error is the same,
Call to a member function num_rows() on boolean
Can anyone help me please?
Tr this
function getName($no_ktp){
$this->db->select('nama')->from('dt_prbd')->where('no_ktp', $no_ktp);
$qry_getName = $this->db->get();
$result = $qry_getName->result();
return $result;
}
}
Try this
In Model
function getName($no_ktp)
{
$query = $this->db->query("SELECT nama FROM dt_prbd WHERE no_ktp= '$no_ktp'");
$result = $query->result_array();
$count = count($result);
if (empty($count))
{
$log = 0;
return $log;
}
else
{
return $result;
}
}
In controller
$result = $this->Model_name->getName();
if($result == 0)
{
echo 'Data Is empty';
}
else
{
$data['name'] = $result;
#rest of your code
}
EDIT 01
To configure your database
go to config/database.php in bottom of page define
Databse name
Mysql Username
Mysql Password
all problem solved, there is no error in code. i try to re-create the db, and then all script turn out like what i expect.
thanks for all help Deep Parekh and Abdulla
I write this query in CI.
$this->db->select("initiation.*, project.projectname,subproject.subprojectname, concat(employee.firstname,' ',employee.lastname) as 'name'")
->from('initiation')
->join('project','initiation.projectid=project.id')
->join('employee','initiation.leaderid = employee.id')
->join('subproject','initiation.subprojectid=subproject.id','left');
$query = $this->db->get();
return $query->result();
It works fine in Firefox, but get this error message in Chrome.
Fatal error: Call to a member function result() on a non-object
Any idea please?
Try with result_array(); Make sure you have autoloaded database library
$this->db->select("initiation.*, project.projectname,subproject.subprojectname, concat(employee.firstname,' ',employee.lastname) as 'name'")
->from('initiation')
->join('project','initiation.projectid=project.id')
->join('employee','initiation.leaderid = employee.id')
->join('subproject','initiation.subprojectid=subproject.id','left');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
$this->db->select("initiation.*, project.projectname,subproject.subprojectname, concat(employee.firstname,' ',employee.lastname) as 'name'")
->from('initiation')
->join('project','initiation.projectid=project.id')
->join('employee','initiation.leaderid = employee.id')
->join('subproject','initiation.subprojectid=subproject.id','left');
$query = $this->db->get();
$result = $query->result_array();//changed
return $result;//changed
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...
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