Error in displaying data from mysql db using php codeigniter - php

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.

Related

getting an error while fetching page title from database using codeigniter php

Fetching page title from database getting error as
A PHP Error was encountered Severity: Notice Message: Trying to get
property of non-object Filename: controllers/digital_marketing.php
Line Number: 20
A PHP Error was encountered Severity: Notice Message: Trying to get
property of non-object Filename: controllers/digital_marketing.php
Line Number: 21
I am having two tables like
1.digital_marketing
2.pagetitle
In the first table i am inserting the data related to digital marketing along with digitalmarketing_name(The table will be in the following format)
digital_id description digitalmarketing_name
1 dfhbsdjbfd digital_marketing
Second Table:(pagetitle)
pagetitle_id page_title title
1 digital_marketing Digital Marketing
In this i am comparing page_title if both the page_titles match then i need to display title name but while comparing that getting an error which i have posted above.
If i am using underscore(_) in the page title it is getting that error if not it is working fine.
Controller:
class Digital_marketing extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('index_model');
$this->load->model('digitalmarketing_model');
}
public function index()
{
$data['records2']=$this->digitalmarketing_model->get_digitalmarketing();
$pageReult = $this->digitalmarketing_model->getpagetitle($this->uri->segment(1));
$data['page_title']=$pageReult->title;
$data['meta_tags']=$pageReult->meta_tags;
$data['mainpage'] = "digital-marketing";
$this->load->view('templates/template',$data);
}
Model:
function getpagetitle($id)
{
$this->db->select('P.*,D.digitalmarketing_name');
$this->db->from('pagetitle AS P');
$this->db->join('digital_marketing AS D','D.digitalmarketing_name=P.page_title','INNER');
$this->db->where(array('P.page_title'=>$id));
$q=$this->db->get();
//var_dump($this->db->last_query());
//print_r($q->num_rows());
if($q->num_rows()>0)
{
$output = $q->result();
return $output[0];
}
else
{
return false;
}
}
The pagetitle which i have inserted in digital_marketing table it is my controller name.
You can change your modal function
Controller.php
public function index()
{
$data['records2']=$this->digitalmarketing_model->get_digitalmarketing();
#echo $this->uri->segment(1); exit;
$pageReult = $this->digitalmarketing_model->getpagetitle($this->uri->segment(1));
$data['page_title']=$pageReult->title;
$data['meta_tags']=$pageReult->meta_tags;
$data['mainpage'] = "digital-marketing";
$this->load->view('templates/template',$data);
}
Modal : digitalmarketing_model.php
function getpagetitle($id) {
$this->db->select('p.*,d.digitalmarketing_name');
$this->db->from('digital_marketing AS d');
$this->db->join('pagetitle as p', 'p.page_title = d.digitalmarketing_name', 'left');
$this->db->where('p.page_title',$id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$row = $query->row();
return $row;
} else {
return false;
}
}
I hope this will helps you.

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.

message undefined variable: news on view page

I'm new to PHP and CodeIgniter, and saw that there is many questions mentioning this and been trying them all but nothing seems to work. Everything is auto-loaded in configuration, the database is running and function for posting to database are working but writing to view page doesn't work at all. Except for displaying username, but for that I create a new variable on view page.
Controller
public function ShowNews()
{
$data = array();
$this->load->model('user');
$data['news'] = $this->user->getNews();
$this->load->vars($data);
}
Model
function getNews(){
$q = $this->db->get('News');
if($q->num_rows() > 0){
return $q->result();
}
return FALSE;
}
View
<?php foreach($news as $row) : ?>
<li><?php echo $row->Title; ?> </li>
<li><?php echo $row->Date; ?></li>
<?php endforeach; ?>
This is the error EDIT ves to news
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: admin/Pocetna.php
Line Number: 64
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: admin/Pocetna.php
Line Number: 64
Using WAMP, NetBeans with CodeIgniter. I saw it has to be something with passing data from controller to view but I can't figure it out and been trying few days already, but always having problems.
You're not passing $data to your view. Your controller should be like this:
public function show_news()
{
$this->load->model('user');
$data = [];
$data['news'] = $this->user->get_news();
$this->load->view('news', $data);
}
Your view should also be checking if $news is FALSE, because you'll have some issues with foreach if you loop over the value FALSE. Your model should also be returning result_array not result, foreach cam't loop over objects..
public function get_news()
{
$q = $this->db->get('News');
return($q->num_rows() > 0) ? $q->result_array() : FALSE;
}
Your view should look something like this:
<?php
if($news !== FALSE)
{
foreach($news as $row)
{
echo "<li>{$row['title']}</li>";
echo "<li>{$row['date']}</li>";
}
}
else
{
echo "No news to see here!";
}
?>
Your title also doesn't link up with the error in the post, so that's the solution to the one in the title.

Illegal string offset error in codeigniter view

im doing a PHP project using codeigniter 2.1.2 , using mySql database on WAMP, i have stumbled upon the following problem:
Here is my model
function loadUserData() {
$sql = "SELECT username FROM user WHERE username = ?";
$query = $this -> db -> query($sql, $this->session->userdata('username'));
if ($query -> num_rows() > 0) {
foreach ($query->result() as $row) {
$data = $row;
}
return $data;
}
}
The controller
function profile_access() {
if ($this -> session -> userdata('is_logged_in')) {
$this->load-> model('profile_model');
$uData['rows']=$this->profile_model->loadUserData();
$this->load->view('profile_view',$uData);
} else {
echo "<script type=\"text/javascript\"> alert(\"You need to provide your credentials first.\");</script>";
redirect('login_controller/index');
}
}
and the view
<?php
foreach($rows as $r){
echo print_r($r);
echo $r[username]->username; //line no. 81 in my code
}
?>
the print_r($r) from view gives me the following errors:
alphaomega1
A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant username - assumed 'username'
Filename: views/profile_view.php
Line Number: 81
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'username'
Filename: views/profile_view.php
Line Number: 81
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/profile_view.php
Line Number: 81
I expect the above code to return only "alphaomega" which is the username in the database. it is however displayed, but i dont know why i'm getting those errors. would appreciate some help with this. thanks.
In your view:
foreach($rows as $r){
echo $r->username;
}
In your model:
function loadUserData() {
$sql = "SELECT username FROM user WHERE username = ?";
$query = $this->db->query($sql, $this->session->userdata('username'));
return $query->result();
}

php returning undefined property

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.

Categories