codeigniter error with selecting data from table - php

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

Related

num_rows() function is not working for MSSQL in codeigniter

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

CI Active record not work in Google Chrome?

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

Redirecting not working in codeigniter

So here's what I want to do. I want to check if the userid in segment(3) exist or else it will redirect somewhere instead of still loading the view with an error.
Here's the example url
http://localhost/ems/edit_user/edit_user_main/1001
Now if I try to edit the userid in segment(3) and intentionally put an invalid userid, it still loads the view and i don't know why
Here's my function
public function edit_user_main(){
$id = $this->uri->segment(3);
$check = $this->get_data->check_if_exist($id);
if($check) {
$data['title'] = 'Edit User';
$data['id'] = $this->session->userdata('usertoedit');
$this->load->model('accounts/get_data');
$item = $this->get_data->get_user($id);
$data['user'] = $item[0];
$data['main_content'] = 'edit_user/edit_user_main';
$this->load->view('includes/template', $data);
} else {
redirect('admin/adminuser');
}
}
Here's the model
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query) {
return TRUE;
} else {
return FALSE;
}
}
There is no problem with the fetching of data.
The problem is even if the userid doesn't exist, the view is still loading but with an error coz there's no data for that userID. It's not redirecting,
I tried using print_r and it working fine, the value of the $check is 1 when there's a valid userID.
Hope someone can help me with this. Thank you
With your function it will always return true because the statement
$this->db->get_where('accounts',array('user_id'=>$id));
will always execute,So you need to check query is returning any result row or not with the statement
$query->num_rows().
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query->num_rows() > 0){ //change made here
return TRUE;
}
else{
return FALSE;
}
}
Try this..
With the function it will always return true because the following statement
$this->db->get_where('accounts',array('user_id'=>$id));
will always be execute, So need to check query is returning any result row or not
$query->num_rows().
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query->num_rows() > 0){ //change made here
return TRUE;
}
else{
return FALSE;
}
}
And load heper as:-
$this->load->helper('url');
before the redirection

Trying to get property of non-object/ Php codeigniter

I can't seem to figure about what is wrong with this code. thank yu for your help.
I am getting error messages:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/members.php
Line Number: 24
My model:
public function show_user()
{
$this->db->select('');
$this->db->from('users');
$this->db->where('email',$this->input->post('email'));
$q=$this->db->get('');
if($q->num_rows() > 0 )
{
$data = array();
foreach($q->result() as $row)
{
$data=$row;
}
return $data;
}
}
My controller:
public function members()
{
if ($this->session->userdata('is_logged_in'))
{
$this->load->model('model_users');
$data['member'] = $this->model_users->show_user();
$this->load->view('members', $data);
}
else
{
redirect('main/restricted');
}
}
My view:
echo "<p> Congratulations you are logged in!!</p>";
foreach($member as $row)
{
echo $row->Name. "<br/>";
echo $row->email. "<br/>";
}
So this my code. I want to display user information from database but I am getting errors. please help me..
You only need to return $q-result();
if($q->num_rows() > 0 )
{
return $q-result();
}
I'm not sure what your database looks like but I suspect this is because either the Name or email properties don't exist on the $row object. Are you sure your field in your users table is called Name or is it name (lowercase)? If its lowercase your code should be.
foreach($member as $row)
{
echo $row->name. "<br/>"; //Changed this line
echo $row->email. "<br/>";
}
Also your foreach loop on your model is a bit pointless. You might as well remove it and just do
return $data->row;

PHP PDO CRUD class Call to a member function rowCount() on a non-object

I'm just getting started using PDO to move away from mysqli but hit a problem. I'm following a tutorial and I want to return an array from the database but I get the following error:
Fatal error: Call to a member function rowCount() on a non-object in C:\xampp\htdocs\phptuts\crud\core\managedb.class.php on line 27
Here is my managedb.php class:
<?php
class ManageDatabase
{
public $link;
function __construct()
{
include_once('database.class.php');
$conn = new database;
$this->link = $conn->connect();
return $this->link;
}
function getData($table_name, $id=null)
{
if(isset($id))
{
$query = $this->link->query("SELECT * FROM $table_name WHERE id = '$id' ORDER BY id ASC");
}
else
{
$query = $this->link->query("SELECT * FROM $table_name ORDER BY id ASC");
}
$rowCount = $query->rowCount();
if($rowCount >= 1)
{
$result = $query->fetchAll();
}
else
{
$result = 0;
}
return $result;
}
}
Then I'm simply using the following code to try and get a response:
<?php
include_once('../core/managedb.class.php');
$init = new ManageDatabase;
$table_name = 'users';
$data = $init->getData($table_name);
print_r($data);
This is when I get the error, Any ideas?
I'd var_dump($query) before the $rowCount = $query->rowCount(); line to see what it actually is, because apparently it's not an object. I'm guessing it's either NULL or empty because the whole $this-link->query(<sql statement>); didn't return what you expected
A couple of things to check out:
From the PHP manual:
PDO::query() returns a PDOStatement object, or FALSE on failure.
You'll want to test if the query succeed and if not, why. You can check the error using PDO's errorInfo function:
if ($query == false)
{
print_r($this->link->errorInfo());
exit();
}
Another thing to note is that rowCount() in PDO returns the affected rows from a INSERT / UPDATE / DELETE type statement. For a SELECT you may get a row count, or you may not. The manual suggests a separate query to find the number of rows, but in your instance it might be easier testing if you get anything back from fetchAll():
$result = $query->fetchAll();
if (!empty($result))
{
return $result;
}
else
{
return 0;
}

Categories