3

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.

1 Answer 1

3

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();
}
Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the quick response @Yan . it worked. But, in my model,wasn't i doing the same thing?, i was just checking whether there are any rows returned or not. what exactly is the difference if u wont mind explaining.
Glad it helped. The difference is that in your model you were erasing $data with each iteration ($data = $row) which made the result a single row. It is a waste of processing time and the outcome is different. If you want to get a single row you can use $query->row().
Another difference: In your function if the number of rows returned in query is zero your function doesn't return a value.
thanks! I also found that if in my model instead of writing $data = $row if i write $data[]=$row and changing the view as you suggested. it works.
Whats the benefit of doing it over simply returning $query->result()?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.