Illegal string offset error in codeigniter view - php

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();
}

Related

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.

Function php argument

something is wrong in one of my functions. The others work fine.
The application has a MVC structure. Beginning with objects and MVC, I must ay.
Basically, I have a page (services.php) linking to:
echo"Plus d'informations...";
Function in the model page (model.php):
function get_NextServ($id)
{
global $bdd;
$id = (int)$id;
$req = $bdd->query('SELECT * FROM entreprises WHERE id= ?');
$req->execute(array($id));
return $req;
}
And the detailservices.php page:
try
{
if (isset($_GET['id']))
{
$id = intval($_GET['id']);
$billet = get_NextServ($id);
if (get_NextServ($id) < 1)
{
echo "No......";
} else
{
foreach ($billet as $data)
{
echo "<h2>" . $data['entreprise'] . "</h2>";
}
.....
I have the following errors:
Warning: Missing argument 1 for get_NextServ(), called in projects\myapp-v3\controllers\controllers.php on line 27 and defined in projects\myapp-v3\model\model.php on line 54
Notice: Undefined variable: id in projects\myapp-v3\model\model.php on line 57
Fatal error: Call to a member function execute() on a non-object in projects\myapp-v3\model\model.php on line 59
Thanks guys!

PHP CodeIgniter: Trying to get property of non-object

Everything seems to work just fine but I keep getting the following error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/ordermodel.php
Line Number: 24
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/ordermodel.php
Line Number: 31
================================================================
In the view i just echo $company_name
Controller:
$city = $this->ordermodel->get_city($order_reference);
$customerCompanyName = $this->ordermodel->get_company_name($order_reference);
$data['company_name'] = $customerCompanyName;
Model:
function get_city($ordernumber) {
$this->db->where('order_number', $ordernumber);
$city = $this->db->get('order');
return $city->row()->city;
}
function get_company_name($ordernumber) {
$this->db->where('order_number', $ordernumber);
$companyname = $this->db->get('order');
return $companyname->row()->company_name;
}
Use therresult() method, it should elimiate all of your errors
$row = $city->result(); return $row->city;
Seem that your query has no results, try:
function get_company_name($ordernumber) {
$this->db->where('order_number', $ordernumber);
$companyname = $this->db->get('order');
if ($companyname->num_rows() > 0) {
return $companyname->row()->company_name;
}
else {
return '';
}
}

Trying to add to an array

I'm trying to pass along the question AND the id of the question in the first query and then with that id get that poll's options and add those to the array. Not seeing what I'm doing wronge here.
Here's the error I am getting:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_DB_mysql_result::$row
Filename: models/sitemodel.php
Line Number: 161
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/sitemodel.php
Line Number: 161
Code:
function getPoll() {
$this->db->select('site_polls.id, site_polls_questions.poll_question');
$this->db->from('site_polls');
$this->db->join('site_polls_questions', 'site_polls_questions.id = site_polls.site_polls_questions_id');
$this->db->where('site_polls.status_id', 1);
$this->db->order_by('site_polls.date_posted', 'desc');
$this->db->limit(1);
$query = $this->db->get();
$id = $query->row->id;
$this->db->select('site_polls_questions_options.poll_option');
$this->db->from('site_polls_questions_options');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->result_array();
}
I'm trying to figure out how I can add the question of the poll into the array.
row() is a function and not a property.
$id = $query->row()->id;
or
$id = $query->row(0)->id;
$id = $query->row->id;
Should be:
$id = $query->row()->id;
$query->row() is a function, not a property.
Try this:
$query = $this->db->get()->first_row();
if ( !empty($query->id))
{
$id = $query->id;
}
else
{
return array();
}

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