【发布时间】:2013-09-30 17:33:13
【问题描述】:
您好,我一直在尝试从我的数据库中检索记录,但我在多个字段中不断收到此错误“严重性:警告消息:非法字符串偏移”。
这是我的控制器 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);
}
}
?>
这是我的模型 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);
}
}
?>
这是我的查看页面 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>
【问题讨论】:
-
你哪里出错了?
-
与手头的问题无关,但您的 MVC 结构有点混乱。你真的应该在控制器中调用你的视图。 问题:您只从数据库返回一行,然后使用
foreach在视图中循环遍历该行列。删除 foreach 并访问$query['id']之类的数据,或者在您的模型中将数据设置为$data['query'] = $results。 -
var_dump($row) 看看它是什么(应该是一个包含您尝试访问的索引的数组)
标签: php codeigniter