CodeIgniter多维数组存储在mysql数据库的单列中 [英] CodeIgniter multidimensional array store in mysql database single column
问题描述
我正在尝试将此数据存储在数据库中,但出现错误.该如何解决?
I am trying to store this data in database but i am getting error. how to fix this?
我只想将多维数组存储在单个列中.
$data = array(
'2017' => array(
'6' => array(
'10' => array(
'count' => 76
),
),
),
);
$getdata = $this->view_count->setView($data);
模型
public function setView($data)
{
$setData = $this->db->where('short', 'honwl')->update('ci_links', $data['view_count']);
return $setData;
}
我遇到的错误 遇到PHP错误
Error which i am getting A PHP Error was encountered
严重性:通知
消息:未定义索引:view_count
Message: Undefined index: view_count
文件名:models/View_count.php
Filename: models/View_count.php
行号:14
回溯:
文件:C:\ wamp \ www \ blog \ application \ models \ View_count.php 行:14 功能:_error_handler
File: C:\wamp\www\blog\application\models\View_count.php Line: 14 Function: _error_handler
文件:C:\ wamp \ www \ blog \ application \ controllers \ Dashbord.php 线:52 功能:setView
File: C:\wamp\www\blog\application\controllers\Dashbord.php Line: 52 Function: setView
文件:C:\ wamp \ www \ blog \ index.php 线:315 功能:require_once
File: C:\wamp\www\blog\index.php Line: 315 Function: require_once
发生数据库错误
您必须使用设置"方法来更新条目.
You must use the "set" method to update an entry.
文件名:C:/wamp/www/blog/system/database/DB_query_builder.php
Filename: C:/wamp/www/blog/system/database/DB_query_builder.php
行号:1864
推荐答案
根据您的示例代码,您可以将数组数据编码为JSON以保存到列,然后通过解码返回:
According to you example code, you can encode array data to JSON for saving to column, then get back by decode:
控制器:
$data = array(
'2017' => array(
'6' => array(
'10' => array(
'count' => 76
),
),
),
);
$getdata = $this->view_count->setView($data);
$data = $this->view_count->getView();
型号:
public function setView($data)
{
$data = json_encode($data);
$setData = $this->db->where('short', 'honwl')->update('ci_links', array('column_name'=>$data));
return $setData;
}
public function getView($data)
{
$row = $this->db->where('short', 'honwl')->get('ci_links')->row();
return json_decode($row->column_name, true);
}
column_name
取决于保存数组数据的表的列名.
The column_name
depends on your table's column name that save the array data.
这篇关于CodeIgniter多维数组存储在mysql数据库的单列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!