Call to member function result() of non object codeigniter - codeigniter

public function get_priority($priority_id = "") {
if ($priority_id == "") {
$qry = $this->db->get("tm_priority");
} else {
$qry = $this->db->query("SELECT * FROM tm_priority WHERE nt_id = {$priority_id}");
echo $this->db->last_query();
}
print_r($qry->result());
}
In my above model function I echo the last_query() for debugging purpose and it's giving me the following result
SELECT * FROM tm_priority WHERE nt_id = 1
When I run this query directly into my mysqlyog, it's working fine.
Then what else could be the reason of the error below
Fatal error: Call to a member function result() on a non-object in /home/staging/erp/application/models/tasks/task_model.php on line 341

You should try to check for number of returned rows before outputting it :
if($qry->num_rows() > 0){
print_r($qry->result());
}

Related

Echo an error message if query result is empty instead of ERRORS (Codeigniter)

My target is to show an echo "No data" when the query result is empty. What happening currently is that when my query is empty, it shows an error. (Please see the picture below for the error) I provided my codes below and my screenshot. Any help will be appreciated. Thank you
Controller:
public function new1($arenaID=0) {
$data['activeNav'] = "";
$data['subnav'] = "arenas";
$this->header($data);
$this->nav();
$data['k1']=$this->arenas->meron1();
$this->load->view('new', $data);
$this->footer();
}
Model:
function meron1(){
$query = $this->db->query("SELECT fightID FROM fight_entries where position='meron' ORDER BY fightID DESC LIMIT 1");
return $query->row()->fightID+1;
}
A better way to do this is just to check the number of rows being returned.
function meron1(){
$query = $this->db->query("SELECT fightID FROM fight_entries where position='meron' ORDER BY fightID DESC LIMIT 1");
if($query->num_rows() > 0){
$query = $query->rows();
return $query->fightID+1;
}else{
$message = "No Data";
return $message;
}

How to check name already exist from first result array

i want to check cpt code is already exist in db or not in update form..
so,First i'm checking all cpt codes present in database except post one now i'm getting result without post cpt code....now I want check cpt code from first query result.
my function in controller
public function updatedDxCodeExist()
{
$this->load->model('MedicineModel');
$Id = $this->input->post("Id");
$dxCode = $this->input->post("dxCode");
if(empty($Id))
{
$result = $this->MedicineModel->updatedDxCodeExist($dxCode);
} else {
$result = $this->MedicineModel->updatedDxCodeExist($dxCode, $Id);
}
if(empty($result))
{
echo("true");
}
else
{
echo("false");
}
}
my function in model
public function updatedDxCodeExist($dxCode, $Id =0)
{
$this->db->select("dx_code");
$this->db->from("dx_codes");
$this->db->where('dx_code !=',$dxCode);
$query = $this->db->get();
return $query->result();
}
Actually you can use like this:
if($result->num_row() <= 0 )
{
echo("true");
}
else
{
echo("false");
}
You can use array_search function in codeigniter

Reading Dates from PostgreSQL in Laravel

I'm trying to create an object in Laravel and when I set the date I get an error.
Code
<?php
$result = pg_query($this->conn, "SELECT * FROM cliente");
if (!$result) {
$this->message('Error to query cliente');
exit;
}
while ($row = pg_fetch_row($result)) {
if ($row[7] == null) {
$cliente->fecha_nacimiento = null;
} else {
$fechaString = (string)$row[7];
$fecha = DateTime::createFromFormat('Y-m-d', trim($fechaString));
$cliente->fecha_nacimiento = $fecha;
}
}
Error
[ErrorException]
DateTime::createFromFormat() expects parameter 2 to be string, object
given
If I use Carbon:
$fecha = Carbon::createFromFormat('Y-m-d', trim($fechaString));
Getting:
[InvalidArgumentException] The separation symbol
could not be found Unexpected data found. Trailing data
Thanks in advance!

Call to a member function row() on a non-object

i am getting error Call to a member function row() on a non-object in codeigniter my controller is
public function edit_survey_pro($id)
{
$id = intval($id);
$survey = $this->model->get("surveys",array("ID" => $id),100000);
if (sizeof($survey) == 0) $this->template->error(lang("error_32"));
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey->row()
)
);
}
my model is
function get($table,$where='',$perpage=0,$start=0,$order_by='',$arr='')
{
$this->db->from($table);
if($perpage != 0 && $perpage != NULL)
$this->db->limit($perpage,$start);
if($where){
$this->db->where($where);
}
if($order_by){
$this->db->order_by($order_by);
}
if($arr=='')
$query = $this->db->get()->result();
else
$query = $this->db->get()->result('array');
if(!empty($query))
if($perpage != 0 && $perpage != NULL)
$result = $query;
else
$result = $query[0];
else
$result = array();
return $result;
}
here loadContent() is just load the content with view path
public function loadContent($view,$data=array(),$die=0){
//something to load the content
}
in my model I am getting the result as an array of object in $query and then it is returned as $result like this -
$query = $this->db->get()->result(); but at the controller $survey stores array of object and i want to show the content of that array of object ,previously I use
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey->row()
)
);
to get that data but the problem is $survey->row() cannot return that data bcoz it is not an object it is array of object so it can't be returned through row() method
so instead of this I just call the first element of that data like this-
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey[0]
)
);
Somehow its works for me bcoz I want to show the first row of the data
if sembody wants to show all data then I think he shuld try logic to increment the key value of that array of object for me it is $survey[] you can use foreach loop for increment the of value of the key element
The problems i see are your model, I will dissect it and add comments to your original code to point out the issues:
function get($table,$where='',$perpage=0,$start=0,$order_by='',$arr='')
//above there are problems, you are setting some of your parameters to
//equal blank, but below, in your conditionals, you are checking if they
// exist. They will always exist if they are set to blank. Fix them by
// setting them = NULL like this:
// get($table,$where=null,$perpage=0,$start=0,$order_by=null,$arr=null)
{
$this->db->select();// <-- you forgot this
$this->db->from($table);
if($perpage != 0 && $perpage != NULL)
//when will $perpage = null? , if never, then you dont need it.
$this->db->limit($perpage,$start);
if($where){
//change this to if(isset($where)). Also why do you use
//curly braces here, but not in the above if statement if only
//one line is affected in your if. I would remove the
//curly braces here.
$this->db->where($where);
}
if($order_by){
// change this to if(isset($order_by)). Same thing as
//above about the curly braces here
$this->db->order_by($order_by);
}
if($arr=='')
// change this to if(isset($arr)).
$query = $this->db->get()->result();
else
$query = $this->db->get()->result('array');
//change this to: $query = $this->db->get()->result_array();
if(!empty($query))
//change the above to if($query->num_rows > 0). Also, here since
//your code body is longer then one line, you will need curly braces
//around your if statement
if($perpage != 0 && $perpage != NULL)
//again, will $perpage ever be NULL? However, why do you need
//this conditional at all, if the limit above is already
//doing this job?
$result = $query;
else
$result = $query[0];
else
$result = array();
return $result;
}
after applying all the changes:
MODEL:
function get($table, $where=null, $perpage=0, $start=0, $order_by=null, $arr=null)
{
$this->db->select();
$this->db->from($table);
if($perpage != 0)
$this->db->limit($perpage, $start);
if(isset($where))
$this->db->where($where);
if(isset($order_by))
$this->db->order_by($order_by);
if(isset($arr)) {
$result = $this->db->get()->result_array();
} else {
$result = $this->db->get()->result();
}
return $result;
}
CONTROLLER
public function edit_survey_pro($id) {
$id = intval($id);
$survey = $this->model->get("surveys",array("ID" => $id),100000);
if (!$survey) {
$this->template->error(lang("error_32"));
} else {
$data["survey"] = $survey;
$this->template->loadContent("user/edit_survey_pro", $data);
}
}
I think when you use $this->db->get(), you need to pass the table name as param like this:
$this->db->get('table_name')->result();

Getting data from model to controller

I do have a question : I cannot pass the data from model to controller
You can see some of my codes, please help me.
It does not work!
this is my model "mymodel.php"
....
$query = $this->db->query("SELECT * FROM `rand` WHERE `used` = 0 LIMIT 0, 1");
if($query){
foreach ($query->result() as $row);
}
$t = "EXAMPLE/{$row->code}";
function wandad() {
return $t;
}
.....
and this is my controller mycont.php
...
$this->load->model('mymodel');
$data['new'] = $this->Mymodel->wandad();
$this->load->view('myview',$data);
...
and this is my view myview.php
....
echo $new;
.....
Clearly you The model is not written properly and to corrent this simple do this
1.) I put a default value on $t
2.) I put the query >> loop on the inside function
wandad
so it can be executed once called from controller
function wandad() {
$query = $this->db->query("SELECT * FROM `rand` WHERE `used` = 0 LIMIT 0, 1");
$t = "";
if($query){
foreach ($query->result() as $row){
$t = "EXAMPLE/{$row->code}".'<br>';
}
}
return $t;
}
Here are several issues into your model
Your Foreach function doesn't do anything
$t is not in the same namespace than wandad()
Function wandad is not defined into your model class
I'm not sure of what you wanna get with wandad() function but here's a pattern.
function yourFunction()
{
/* This will return the full query as an array */
$query = $this->db->query("SELECT * FROM `rand` WHERE `used` = 0 LIMIT 0, 1")->result_array();
/* Store variable in the same class */
$this->t = "EXAMPLE/".$query[0]['code'];
/* Close yourFunction() */
}
public function wandad() {
return $this->t;
}
Then into your controller, do that instead :
$this->load->model('mymodel');
$this->mymodel->yourFunction();
$data['new'] = $this->mymodel->wandad();
$this->load->view('myview',$data);
#Touki his foreach actually does something. It will set the variable $row with the last row that is returned from the query. Not the best way to do it ... But it's a way. Better would be to use a limit in the query.
There is a small mistake in your code #Ernesto.that is
foreach ($query->result() as $row){
$t. = "EXAMPLE/{$row->code}".'<br>';
}
but your code was simply nice

Resources