php returning undefined property - php

In other of not be repetitive, I wrote that getName function to do the duty job for me: the classes querying the database would only send the SQL query:
public function getVolumeServer($id)
{
$str = "SELECT *FROM site_volume_server WHERE site_id='$id'";
return $this->getName($str);
}
public function getName($str)
{
$data = array();
$query = $this->getQuery($str);
if($query){
foreach($query->result() as $row){
array_push($data, $row->name);
}
}
return $data;
}
public function getQuery($str){
$data = array();
$query = $this->db->query($str);
if(!$query || $query->num_rows() == 0){
return false;
}
return $query;
}
However, I'm getting the following error:
<p>Severity: Notice</p>
<p>Message: Undefined property: stdClass::$name</p>
<p>Filename: models/sitematrix_model.php</p>
<p>Line Number: 129</p>
Line 129 is $this->getName($str).
I did not have that before, when that portion of code was embedded in the function, but there are similar functions that only differ by the table being accessed.
Any Thoughts?
Thanks

I am having the same problem except I am trying to access the $agenda->time_from property from __get() which is a time column in agenda table which is retrieved from db properly (I checked the var_dump, ran the query manually in phpMyAdmin).
I do that because the default call parent::_get() won't return the value; it returns NULL instead.
I found a workaround for my problem. All you need to do is to access the value like :
$agenda->_object['time_from']
I guess this is not quite the right solution, but working one.

Related

Codeigniter 2: Why does $this->db->get('documents') return false?

I just started using CodeIgniter 2 and probably I have overseen something obvious but I can't figure it out.
My MySQL Database has a table named Documents with a column named online_since.
Function _getMaxOnlineSince() should get the maximum online_since value and return it as a string.
In application/models/browse_model.php:
private function _getMaxOnlineSince() {
$this->db->select_max('online_since');
$oQuery = $this->db->get('documents');
return $oQuery->num_rows() > 0 ? $oQuery->row()->online_since : false;
}
And I get an error message in the Browser:
Fatal error: Uncaught Error: Call to a member function num_rows() on
boolean
because of $oQuery is false: var_dump($oQuery) returns bool(false)
var_dump($this->db->select_max('online_since')) returns object(CI_DB_mysqli_driver)#14 (73) {...}
The Codeigniter 2 application worked on the old server with PHP5.3 but after the update to PHP 7 it crashes on this point.
Why does $this->db->get('documents') return false and how can I fix it?
I could manage to get this function work with sql queries:
private function _getMaxOnlineSince() {
$oQuery = "select max(online_since) from documents";
$sMaxOnlineSinceDate = $this->db->query($oQuery);
return $sMaxOnlineSinceDate->num_rows() > 0 ? $sMaxOnlineSinceDate->row()->online_since : false;
}
But there are many more places where I use
$this->db->get('documents')
I suppose in my case CodeIgniter has trouble to evaluate this statement for some reason. I'll debug further.
You can modify your function _getMaxOnlineSince() in below manner
private function _getMaxOnlineSince() {
$this->db->select_max('id','online_since');
$res1 = $this->db->get('documents');
if ($res1->num_rows() > 0) {
return $res1->row();
}
else {
return false
}
return NULL;
}
Also please check whether the table name documents is correct. Hope this will help you

Always getting error when cart is empty, Laravel 5

Facing this error:
ErrorException in CartController.php line 35: Trying to get property of non-object
This is the code:
public function index()
{
$this->data['details'] = Cart::content();
$this->data['shipping'] = Shipping::where('region_category_id',session('location'))->where('type',session('type_komoditi'))->first();
$regType = session('regType');
$regId = session('id_wilayah');
$qReg = RegionCategory::find($regId);
if($regType == 'children') {
$this->data['minimalWeight'] = $qReg->minimal_weight;
//$this->data['minimalBuy'] = $qReg->min_buy;
}
$this->data['minimalBuy'] = $qReg->min_buy; //this is line 35
$this->data['regType'] = $regType;
\Session::put('price',Cart::total());
\Session::put('totalPrice',Cart::total());
\Session::put('paycode',0);
return view('client.carts.index',$this->data);
}
when I delete the line 35 the error is gone, but that code is important for using minimal buy filter.
How to resolve this error?
That's error cause $qReg->min_buy return null.
You can fix by return default value like this:
$this->data['minimalBuy'] = $qReg->min_buy ?? 0;
That's mean if is null return 0
It's saying that $qReg is not an object and you are trying to get the value from it using min_buy property.
Try printing the object $qReg using print_r($qReg); and exit the script after that.
print_r($qReg);
exit();
And check whether that object has the property min_buy or not.
Why is $this->data['minimalBuy'] = $qReg->min_buy; after if block ? It's being overwritten even if if($regType == 'children') is false.

Undefined property: stdClass in my codeigniter controller

This is my controller function
public function check_orgn_mail()
{
$this->load->model('registrationmodel', '', TRUE);
$omail = $this->input->post('mailid');
$mailresult = $this->registrationmodel->check_org_mail();
$smil = explode("#", $omail);
$registerCompanyID = null;
if ($mailresult)
{
foreach ($mailresult as $row)
{
$registerCompanyID = $row->id;
$cmail = explode("#", $row->email);
$acmail[] = $cmail[1];
}
if (in_array($smil[1], $acmail))
{
return TRUE;
}
}
$this->form_validation->set_message('check_orgn_mail', 'Invalid Organization Mail id.');
return false;
}
This is the function in my model
function check_org_mail()
{
$this->db->select('email','id');
$this->db->from('customers');
$this->db->where('status', 1);
$query_emai = $this->db->get();
return $query_emai->result();
}
I am getting this error
Severity: Notice
Message: Undefined property: stdClass::$id Filename:
controllers/registration.php
I tried some trial and error but not working. Please help me with this. Previously it was working fine but when I checked with the different mail id, it started giving this error.
change your select to this:
$this->db->select('email, id');
You need to use
$this->db->select('email, id');
All columns are supposed be a single string argument separated by commas.
You are providing multiple arguments.

CodeIgniter $data not passed from Controller to View

my controller is not passing $data to my view and I don't know why not. I'm reusing code from a previous project which worked fine and I certainly understand the idea of how $data passing is meant to work. But maybe I missed something when copying code over?
I put in the variable $data['hello'] in there just for testing purposes. As you can see from the output $hello isn't even getting through. The if fails and the else code is run correctly which means the view file itself is being loaded.
Controller:
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
View:
<?php
echo $hello;
if ($users->num_rows != 0) {
foreach ($users->result() as $user) {
}
} else {
echo "No users.";
}
Output (abridged):
A PHP Error was encountered
Message: Undefined variable: hello
Line Number: 2
A PHP Error was encountered
Message: Undefined variable: users
Line Number: 3
A PHP Error was encountered
Message: Trying to get property of non-object
Line Number: 3
No users.
Edit: more info on request:
Model:
public function get_users($amount = 0, $offset = 0) {
$this->db->from('users');
$this->db->order_by('l_name', 'desc');
if ($amount != 0)
$this->db->limit($amount, $offset);
return $this->db->get();
}
I always do like this
change your model to
$query = $this->db->get();
return $query->result();
And in view
if (count($users)> 0) {
foreach ($users as $user) {
echo $user['name'];
}
} else {
echo "No users.";
}
Hope this helps
Regards
iijb
Just write $data = array(); before you are sending some data into $data array.
function users() {
$data = array();
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
I think you could solve your issue with some simple var_dump() checks.
Check what's coming out of your model by var_dump()ing $data['users'] - is this an object? What happens when you var_dump() $data['users']->result()?
Then, var_dump() $data in your view - does it have all the pieces?
Thing is, even showing us your model function doesn't prove that your getting a real data result. Check that. Your code looks okay at a glance so I don't think that is where the issue exists.
There is something very basic that is wrong. So get out of that controller and do a sanity check. First confirm that your welcome view is working. If it is go to the welcome controller and put this in the index method
$data['here'] = 'we are here' ;
$this->load->view('welcome_message', $data);
and then somewhere in the welcome.php view file
<?php echo $here ?>
You do not need to set this: $data = array();
However some people suggest it because that way even if you dont create any data variables you wont get an error if its in the view call $this->load->view('welcome_message', $data);
finally i would suggest looking at this
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
lets see you have method called users, returning an object called users, and a view called users -- that could get confusing ! :-)

Error in displaying data from mysql db using php codeigniter

I am trying to display data from db. It displays this error
<p>Severity: Notice</p>
<p>Message: Trying to get property of non-object</p>
<p>Filename: controllers/schedule.php</p>
<p>Line Number: 57</p>
<p>Severity: Notice</p>
<p>Message: Trying to get property of non-object</p>
<p>Filename: controllers/schedule.php</p>
<p>Line Number: 58</p>
here is my code in controller which, this code is related to this problem
$data['rows'] = $this->Model_scheduleSave->save_schedule($data);
$this->load->view('home_view',$data);
here is my view
if($rows) {
//var_dump($rows);
foreach(rows as $r) :
"<div>". $r->client_url ."</div>";
"<div>". $r->admin_url ."</div>";
endforeach;
}
here is my model
public function save_schedule($data) {
$query = "INSERT INTO schedule VALUES('',?,?,?,?,?,?,?)";
$result = $this->db->query($query,$data);
if($result) {
$get_urls = "SELECT client_url,admin_url FROM schedule ORDER BY id DESC LIMIT 1";
$results = $this->db->query($get_urls);
if($results) {
foreach($results->result() as $rows) {
$data[] = $rows;
}
}
return $data;
} else {
return false;
}
}
Where am I making mistake?
In case your database query returns no result, the function will return the parameters passed to the function ($data). That might cause your error.
Another word of notice: Don't re-use variable names. You use $data to pass parameters to your function in the controller, and afterwards you assign the result to $data['rows']. You should use two variables, one for the view variables, and one for the function parameter.

Categories