没有从数据库Codeigniter获取任何数据

时间:2014-09-12 06:16:24

标签: php codeigniter

我仍然无法使用codeigniter处理我的变量。

我所追求的只是能够做到这一点echo $this->configs->getData('config_meta_title');并且会在该行上返回该值

我尝试了很多方法,但没有运气。 无法获取任何数据?

public function getData($key) {

    // Group = Config 

    // Key Would Be Example $this->configs->getData('config_meta_title');

    // echo $this->configs->getData('config_meta_title'); returns value on this row.

    $query = $this->CI->db->get_where('setting', array('group' => 'config', 'key'=> $key))->result_array();


    if ($query->num_rows > 0) {
        return  $query->row('value');
    } else {
        return false;
    }
   }

2 个答案:

答案 0 :(得分:0)

尝试这个不久的代码,它会完美地工作..

  $this->CI->db->select('*');
 $this->CI->db->from('settings');
 $this->CI->db->where(array('group' => 'config', 'key'=> $key));
$query = $this->CI->db->get();
  return $query->result();

答案 1 :(得分:0)

我现在已经制作了库文件,所以一切正常工作我不得不添加一些东西,现在只是在模型到套件上工作

控制器

if (trim($this->input->post('config_meta_title'))) {
    $data['config_meta_title'] = $this->input->post('config_meta_title');
} else {
    $data['config_meta_title'] = $this->configs->get('config_meta_title');
}

库     

class Configs {

    private $data = array();

   public function __construct(){        
        $this->CI =& get_instance();
   }

    public function get($key) {

   if ($key) {

      $value = $this->get_config($key);

      if ($value) {

         return $value;

      } else {

         return false;

      }

      } else {

         return false;

      }

        return (isset($this->data[$key]) ? $this->data[$key] : null);
    }

    public function set($key, $value) {
        return $this->set_key($key, $value);
    }

    public function has($key) {
        return isset($this->data[$key]);
    }

    function get_config($key) {
      if ($key) {

      $data = $this->CI->db->get_where('setting', array('group' => 'config', 'key' => $key))->result_array();

      if (!empty($data)) {

      return $data[0]['value'];

      } else {

      return false;

      }

      } else {

         return false;

      }
   }

   function set_key($key, $value) {

      $checkExitKey = $this->CI->db->get_where('setting', array('group' => 'config', 'key' => $key))->result_array();

      if (!empty($checkExitKey)) {

         return $this->CI->db->update('setting', array('value'=>$value), array('group' => 'config', 'key' => $key));  

      }  else {

         $data = array(
            'website_id' => 0,
            'group' => 'config',
            'key' => $key,
            'value' => $value
         );

         return $this->CI->db->insert('setting', $data);
      }
    }


}
相关问题