CodeIgniter Forums
Undefinded Offset: 0! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Undefinded Offset: 0! (/showthread.php?tid=58546)



Undefinded Offset: 0! - El Forum - 06-22-2013

[eluser]@MaxG[/eluser]
Dear Forum,

When I send a Request to this Page including POST-DATA ({"bot_hw_id":"2147483647"}), i get the following error:
Code:
<p>Severity: Notice</p>
<p>Message:  Undefined offset: 0</p>
<p>Filename: models/prometheus_model.php</p>
<p>Line Number: 26</p>

My Code:

Controller(update_bot function):
Code:
public function update_bot()
{
  $bot_data = json_decode($this->input->post('bot_data'));
  $to_update = array(
  'bot_last_update' => time(),
  'bot_ip' => $_SERVER['REMOTE_ADDR'],
  'bot_port' => $_SERVER['REMOTE_PORT']
  );
  $bot_data = $this->prometheus_model->get_bot_data_by_hw_id(/*$bot_data->{'bot_hw_id'}*/$bot_data->{'bot_hw_id'});
  //echo $bot_data['bot_id'];
  print_r($bot_data);
  $this->prometheus_model->update('bots', array('bot_id' => $bot_data['bot_id']), $to_update);
  //var_dump($bot_data);
    
    
}

Model(prometheus_model):

Code:
&lt;?php
class Prometheus_model extends CI_Model {

var $tables = array(
  'bots' => 'bots'
);

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

public function tablename($table = NULL) {
  if(! isset($table)) return FALSE;
  return $this->tables[$table];
}

public function get($table, $where = array(), $single = FALSE, $order = NULL) {
  $this->db->where($where);
  if(isset($order)) {
   $this->db->order_by($order);
  }
  $q = $this->db->get_where($this->tablename($table),$where);

  $result = $q->result_array();
  if($single) {
   return $result[0];
  }
  return $result;
}

public function update($table, $where = array(), $data) {
  $this->db->update($this->tablename($table),$data,$where);
  return $this->db->affected_rows();
}

public function insert($table, $data) {
  $this->db->insert($this->tablename($table),$data);
  return $this->db->insert_id();
}

public function delete($table, $where = array()) {
  $this->db->delete($this->tablename($table),$where);
  return $this->db->affected_rows();
}

public function explicit($query) {
  $q = $this->db->query($query);
  if(is_object($q)) {
   return $q->result_array();
  } else {
   return $q;
  }
}

public function num_rows($table, $where = NULL) {
  if(isset($where)){
  $this->db->where($where);
  }
  $q = $this->db->get($table);
  return $q->num_rows();
}

public function get_bot_data_by_hw_id($bot_hw_id) {
  $q = $this->get('bots', array('bot_hw_id' => $bot_hw_id), TRUE);
  return $q;
}

}

?&gt;



Undefinded Offset: 0! - El Forum - 06-22-2013

[eluser]greedyman[/eluser]
Where is Line Number: 26?


Undefinded Offset: 0! - El Forum - 06-22-2013

[eluser]@MaxG[/eluser]
if($single) {
return $result[0];
}


Undefinded Offset: 0! - El Forum - 06-24-2013

[eluser]Pert[/eluser]
$result[0] has not been assigned. When dealing with arrays, you should check the index exists as well.

Code:
if ($single && isset($result[0]))
{
   return $result[0];
}