简体   繁体   English

SQL从查询中选择返回错误的信息,codeigniter

[英]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. 我正在使用codeigniter构建一个应用程序,此应用程序有一个配置文件页面,其中显示的所有信息均取决于当前登录的用户。在配置文件页面上,我试图设置一个生物盒,以便每个用户都可以编写自己的信息唯一的生物,这将存储在数据库中。 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. 我一直无法解决问题所在,我对Codeigniter和php还是很陌生,所以我不具备解决此问题的知识。

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() 您应该使用echo而不是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. var_dump(混合$ expression [,混合$ ...]):void此函数显示有关一个或多个表达式的结构化信息,包括其类型和值。 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). 除非对象实现__debugInfo()方法(在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. echo根据我们在站点中的要求直接打印变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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