0

im having trouble here getting value from a record. Here's what i want to do: 1. Getting a record from db 2. Convert the query result to array 3. Display the array[0], but got error undefined offset:1

The var_dump() : array(1) { [0]=> object(stdClass)#23 (1) { ["nama"]=> string(9) "Test Name" } }

Tried the query by myself, and it return a record : |username|nama | |Testuser|Testnama|

so how can i get the 'Testuser'?

model/verify_login_model

function getinfo($username) {
    $this->db->select('nama', 'username');
    $this->db->from('tb_userInfo');
    $this->db->where('username', $username);
    $this->db->limit(1);

    $result = $this->db->get();
    if($result->num_rows()==1) {
        return $result->result();

    } else {
        return false;
    }
}

controller/verify_login

public function index(){
    $this->load->model('verify_login_model');
    $username = $this->input->post('username');
    $password = $this->input->post('password');
    $result = $this->verify_login_model->verify($username, $password);
    if($result == $username) {

        $row = $this->verify_login_model->getinfo($username);
        echo var_dump($row);
        $sessiondata = array('name'=>$row[0], 'username'=>$row[1]);
        $this->session->set_userdata($sessiondata);
        $name = $sessiondata['name'];
        $this->load->view('home_view', $name);

    } else {
        redirect('login');
    }
}

view/home_view

<html>
<head></head>
<body>
    welcome, <?php echo $name?>
</body>
</html>
  • you can `echo $this->session->userdata['name']` inside of view and about model you can try and `return $result->result_array();` and then inside of controller do `$row[0]['nameofyourcolumn'];` – kunicmarko20 Mar 22 '16 at 17:04
  • @MarkoKunić could you explain me about the var_dump()'s result? why am i getting that stdClass#23 – Ridle Sambow Mar 22 '16 at 17:19
  • Thave you tried return $result->result_array(); instead of return $result->result(); in model – Mr. ED Mar 22 '16 at 18:08
  • @wolfgang1983 the result is : array(1) { [0]=> array(1) { ["nama"]=> string(9) "Test Name" } } – Ridle Sambow Mar 22 '16 at 18:31

2 Answers2

0

your model returns an array of objects, as you use

return $result->result();

in order to get the result as a plain array use:

return $result->result_array();

read more about generating query results here

Vickel
  • 6,356
  • 6
  • 30
  • 49
  • yea thank you, now i stuck at this var_dump :array(1) { [0]=> array(1) { ["nama"]=> string(9) "Test Name" } } after i changed to your suggestion. so why exactly the $row value is kinda array inception? – Ridle Sambow Mar 22 '16 at 19:22
  • check here: http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/ – Vickel Mar 22 '16 at 19:32
  • check the usage in my answer under For result_array() – Kavvson Empcraft Mar 22 '16 at 19:34
  • Thank you for the result_array(), i think your answer is the most accurate as to the title saying about stdClass. now the problem is about the null i got for the row[1]. couldcha point the reason why ? or guess need to post another question – Ridle Sambow Mar 22 '16 at 19:35
  • I think better to ask a complete new question – Vickel Mar 22 '16 at 19:42
0

According to CI query result displays

https://ellislab.com/codeigniter/user-guide/database/results.html

->result() This function returns the query result as an array of objects,

What about the stdClass - check out here https://stackoverflow.com/a/931419/2513428

To get your result you have to do as following

$sessiondata = array('name'=>$row[0]->nama, 'username'=>$row[0]->yourfield);

To visualise this a bit it's like array(1) { [0]=> object(stdClass)#23 (1) { Your query result } } and to get the data you point the object $var[0]->Object. But you can also change to the result_array() and treat it as an array not an object as it is now.

For result_array()

 $sessiondata = array('name'=>$row[0]['nama'], 'username'=>$row[0]['yourfield']);

Hope this will help you.

Community
  • 1
  • 1
Kavvson Empcraft
  • 425
  • 5
  • 30