php 非法字符串偏移codeigniter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18999605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:38:33  来源:igfitidea点击:

Illegal string offset codeigniter

phpcodeigniter

提问by Evans Gacheru

Hi been trying to retrieve records from my database, but I keep getting this error "Severity: Warning Message: Illegal string offset " in several fields.

您好,我一直在尝试从我的数据库中检索记录,但我一直在多个字段中收到此错误“严重性:警告消息:非法字符串偏移量”。

Here's my controller view_logs.php

这是我的控制器 view_logs.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class View_Logs extends CI_Controller {

function View_Logs()
 {
   parent::__construct();
 }  

function Logs(){
    $id = $this->uri->segment(3);

    $this->load->model('log_listmodel');
    $this->log_listmodel->log_list_get($id);    
}
}
?>

Here's my Model log_listmodel.php

这是我的模型 log_listmodel.php

<?php
class Log_Listmodel extends CI_Model{

    function Log_Listmodel()
    {
      parent::__construct();
    }

    function log_list_get($id){
    $query = $this->db->get_where('test_request_log', array('test_request_id' => $id));
    //return $query->result();
    $results=$query->result_array();


    $data['query']=$results[0];
    $this->load->view('logs_list_view',$data);
    }
}
?>

Here's my view page log_list_view.php

这是我的视图页面 log_list_view.php

<table class="list_header" bgcolor="#ffffff" border="0" width="1020px" cellpadding="4px">

            <?php foreach($query as $row): ?>
            <tr> 
                <td><b>Updated</b></td>
                <td><?php echo $row['id'];?>.</td>
                <td><?php echo $row['new_testing_reason'];?></td>
                <td><?php echo $row['new_applicant_name'];?></td>
                <td><?php echo $row['new_authorizer_name'];?></td>
                <td><?php echo $row['new_received_by'];?></td>
                <td><?php echo $row['new_test_required'];?></td>
                <td><?php echo $row['new_laboratory_number'];?></td>
                <td><?php echo $row['log_date'];?></td>
                <td><?php echo $row['who'];?></td>
            </tr>
            <?php endforeach; ?>
        </table>

回答by mgrueter

You've set $data['query']to the first row of your result, but in the view you're using it as it would have the whole data set.

您已设置$data['query']为结果的第一行,但在视图中您正在使用它,因为它将包含整个数据集。

So you need to change

所以你需要改变

$data['query']=$results[0];

to

$data['query']=$results;

or

或者

$data['query']=$query->result_array();

回答by Jhonathan H.

Your code is kind of unproperly structure, I just reconstruct it and make it simple. Hope it works.

您的代码结构不正确,我只是对其进行了重构并使其变得简单。希望它有效。

First lets load your database lib.

首先让我们加载您的数据库库。

Go toapplication/config/autoload.phpfind this line then autoload database library

转到application/config/autoload.php找到这一行然后自动加载数据库库

/*
| -------------------------------------------------------------------
|  Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in the system/libraries folder
| or in your application/libraries folder.
|
| Prototype:
|
|   $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/

$autoload['libraries'] = array('database');

Controller View

控制器视图

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class View_Logs extends CI_Controller {

function View_Logs()
 {
   parent::__construct();
   $this->load->library('database');   // if you didn`t load 'database' in your autoload.php
   $this->load->model('log_listmodel');
 }  

function Logs(){
    $id = $this->uri->segment(3);


    $data['query'] = $this->log_listmodel->log_list_get($id)->result();

    $this->load->view('logs_list_view',$data);
}
?>

Model View

模型视图

<?php
class Log_Listmodel extends CI_Model{

    function Log_Listmodel()
    {
      parent::__construct();
    }

    function log_list_get($id){
        return $this->db->get_where('test_request_log', array('test_request_id' => $id));

    }
}
?>

View Mode

查看模式

<table class="list_header" bgcolor="#ffffff" border="0" width="1020px" cellpadding="4px">

            <?php foreach($query as $row): ?>
            <tr> 
                <td><b>Updated</b></td>
                <td><?php echo $row->id;?>.</td>
                <td><?php echo $row->new_testing_reason;?></td>
                <td><?php echo $row->new_applicant_name;?></td>
                <td><?php echo $row->new_authorizer_name;?></td>
                <td><?php echo $row->new_received_by;?></td>
                <td><?php echo $row->new_test_required;?></td>
                <td><?php echo $row->new_laboratory_number;?></td>
                <td><?php echo $row->log_date;?></td>
                <td><?php echo $row->who;?></td>
            </tr>
            <?php endforeach; ?>
</table>