我正在使用CodeIgniter并收到以下错误:尝试获取非对象的属性。我100%肯定它,因为查询没有找到数据库中的现有行,因为它什么时候工作正常。我是CodeIgniter的新手,不知道如何解决这个问题?
这是我模型中的功能
function getSent(){
$this->db->where('submit_time', $this->uri->segment(3));
$tempsent = $this->db->get('0_request_details'); //this is the problem here. If it doesnt find the row from the database above it doesnt know what to do here
if($tempsent){
$data['sent'] = $tempsent->row();
}
return $data;
}
答案 0 :(得分:0)
试试这个,添加一个IF
function getSent()
{
$this->db->where('submit_time', $this->uri->segment(3));
$tempsent = $this->db->get('0_request_details');
if ( $tempsent->num_rows() > 0 )
{
// If a row is found...
$data['sent'] = $tempsent->row();
return $data;
}
else
{
return false;
}
}
答案 1 :(得分:0)
试试这个......:)
$this->db->select('field_1 , field_2');
$this->db->where('field_name' , $this->uri->segment(3));
$tempsent = $this->db->get('table_name');
$data = (object) NULL;
if($tempsent->num_rows() > 0)
{
$data->sent = $info->result();
}
return $data;
答案 2 :(得分:0)
这里有一些建议 -
是您的数据库表,真正称为' 0_request_details' ??? 有一个数字启动数据库表名称可能是一个问题。 对于这个例子,我会称之为“request_details”'
首先检查并确认$ this-> uri-> segment(3)中的值。如果它是空白或格式错误你想知道。并且您不想将其提交到数据库 - 您想要一个明确的错误,让您知道该问题。
不要将数据库结果附加到模型中的$ data。在你的控制器中做到这一点对于更复杂的任务,您将有多个数组/对象转到$ data - 您希望能够在控制器中清楚地看到它。
不要忘记说明要返回的数据库表字段。
// example your model is called request
// check if we got a submit_time back
// if we don't get a submit_time back then its invalid
if ( ! $submit_time = $this->request->returnValidatedSubmitTime ) {
$this->showInvalidSubmitTime($submit_time) ; }
// $submit_time is valid, now try to get a database record
// check if we got a record back
elseif( ! $data['sent'] = $this->request->getSentWith($submit_time) ) {
$this->showNoRecordsFor($submit_time) ; }
else {
// Success
// $data['sent'] has been set and available to views
}
在模型中
function returnValidatedSubmitTime(){
if($this->uri->segment(3) ) {
$time = $this->uri->segment(3) ;
// do some validation
// if its valid
return $time;
}
else { return false ; }
}
function getSentWith($submit_time){
// the fields that will be returned
$this->db->select( 'id, firstname, lastname' );
$this->db->where('submit_time', $submit_time);
$query = $this->db->get('request_details');
// we are only expecting one row so thats what we check for
if ( $query->num_rows() == 1 ) {
return $query->row(); }
else
// no results
{ return FALSE; }
}
答案 3 :(得分:0)
这是您的代码
function getSent(){
$this->db->where('submit_time', $this->uri->segment(3));
$tempsent = $this->db->get('0_request_details'); //this is the problem here. If it doesnt find the row from the database above it doesnt know what to do here
if($tempsent){
$data['sent'] = $tempsent->row();
}
return $data;
}
你要做的就是拖动数组结果;你可以用两种方式做到这一点
function getSent(){
$this->db->where('submit_time', $this->uri->segment(3));
$tempsent = $this->db->get('0_request_details'); //this is the problem here. If it doesnt find the row from the database above it doesnt know what to do here
if($tempsent){
foreach($tempsent->result() as $ts)
{
$data['sent']=$ts;
}
}
return $data;
}
其次,您可以在查询末尾附加 - > result()以获得指向的结果,然后将输出用作数组索引。
希望不知怎的,你有了这个想法