gpt4 book ai didi

php - codeigniter View 中的非法字符串偏移错误

转载 作者:搜寻专家 更新时间:2023-10-31 20:46:59 24 4
gpt4 key购买 nike

我正在使用 codeigniter 2.1.2 做一个 PHP 项目,在 WAMP 上使用 mySql 数据库,我偶然发现了以下问题:

这是我的模型

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

}

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

和 View

<?php
foreach($rows as $r){
echo print_r($r);
echo $r[username]->username; //line no. 81 in my code
}
?>

View 中的 print_r($r) 给我以下错误:

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

我希望上面的代码只返回数据库中的用户名“alphaomega”。但是它显示了,但我不知道为什么我会收到这些错误。将不胜感激一些帮助。谢谢。

最佳答案

在你看来:

foreach($rows as $r){
echo $r->username;
}

在你的模型中:

function loadUserData() {
$sql = "SELECT username FROM user WHERE username = ?";
$query = $this->db->query($sql, $this->session->userdata('username'));

return $query->result();
}

关于php - codeigniter View 中的非法字符串偏移错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12035392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com