简体   繁体   中英

SQL select from query returns incorrect information, codeigniter

I'm building a application using codeigniter, this application has a profile page where all the information displayed is dependant on the user currently logged in. On the profile page I'm trying to set up a bio box so each user can write their own unique bio, this would be stored in the database. The code I've currently written is displaying a result on the page, but its not the correct result I was hoping for. The code I've written that relates to the problem is below.

In the model:

public function get_profile_information() { 

    $query = $this->db->query("SELECT profileText from users WHERE 
    idNumber = ?", $this->session->userdata('username'));

    return $query->result_array();
}

In the controller:

public function loginprocess() { 

    $username = $this->input->post('idNumber');
    $password = $this->input->post('passWd');

    $query = $this->db->query("select * from users where idNumber='".$username."' and passWd='$password'");

    $row = $query->num_rows();

    if ($row) {

        $this->session->set_userdata(array('username' => $username));
        $sessionID = $this->session->userdata('username');

        $data['title'] = 'Profile';
        $data['profiletext'] = $this->news_model >get_profile_information();
        $data['sessionID'] = $this->session->userdata('username');

        $this->load->view('templates/header', $data);
        $this->load->view('news/profile', $data);
        $this->load->view('templates/footer');

    } else { 

        $data['error'] = 'Invalid Login Details';
        $data['title'] = 'Login';

        $this->load->view('templates/header', $data);
        $this->load->view('news/login', $data);
        $this->load->view('templates/footer');
    }
}

In the view:

<head>

<style type="text/css">

</style>

</head>

Welcome to the <?php echo $title; ?> page, <?php echo $sessionID; ?>

<?php var_dump($profiletext); ?>

<?php echo anchor('login/logout', 'logout'); ?>

On the webpage, the query to get the profile text from the database based on the logged in user gives this result

array(1) { [0]=> array(1) { ["profileText"]=> string(17) "Test Profile Text" } } 

I was hoping just to get the "Test Profile Text" displayed. I've been unable to work out what is wrong, I'm fairly new to codeigniter and php so I don't have the knowledge to fix this issue.

Controller:
Make little change in below line

//$data['profiletext'] = $this->news_model >get_profile_information();//old line
$data['profiletext'] = $this->news_model->get_profile_information();//changes

Model:
To get only "Test Profile Text", without any change in your view

public function get_profile_information() { 

    $query = $this->db->query("SELECT profileText from users WHERE idNumber = ".$this->session->userdata('username'));
    return $query->row('profileText');//changes
}

You should use echo instead of var_dump()

Description:

var_dump ( mixed $expression [, mixed $... ] ) : void This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.

All public, private and protected properties of objects will be returned in the output unless the object implements a __debugInfo() method (implemented in PHP 5.6.0).

Replace

<?php var_dump($profiletext); ?>

By

<?php echo $profiletext[0]['profileText]; ?>

echo prints the variable(s) directly, as we require in our sites.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM