[英]How to insert data using mysql encode function in codeigniter
I am using Codeigniter and I want to encode my string using encode()
function in Mysql. 我正在使用Codeigniter,并且想在Mysql中使用
encode()
函数对字符串进行encode()
。 Is there a way to use that function using $this->db->insert($table_name, $data);
有没有办法使用
$this->db->insert($table_name, $data);
使用该功能$this->db->insert($table_name, $data);
in Codeigniter? 在Codeigniter中? Here is my sample codes:
这是我的示例代码:
CI controller: CI控制器:
$data_array('id' => 1, 'name' => 'Juan Dela Cruz');
$this->load->model('data_mdl');
$this->data_mdl->_insert($data_array);
CI model: CI模型:
public function _insert($data) {
return $this->db->insert($table_name, $data);
}
Any help would be appreciated. 任何帮助,将不胜感激。 Thanks.
谢谢。
Why don't you simple encode your data then send it. 为什么不对数据进行简单编码然后发送。
$this->db->insert($table_name, encode($data));
or 要么
$data = array();
$data = encode($data);
$this->db->insert($table_name, $data);
or 要么
$data = array();
$data = 'ENCODE('. $data .')';
$this->db->insert($table_name, $data);
EDITED FROM HERE: The MYSQL function Encode() just as the name suggests encodes the data, you then unencode that data using the reverse function. 从此处进行编辑:正如名称所暗示的那样,MYSQL函数Encode()对数据进行编码,然后您可以使用反向函数对数据进行解编码。
My suggestion would be to use something simple as base64_encode which works with the same principle with a reverse function, base64_decode. 我的建议是使用诸如base64_encode的简单名称,该名称与反向功能base64_decode的原理相同。
In that case you would use this following code: SOLUTION 1 在这种情况下,您将使用以下代码: 解决方案1
CI controller: CI控制器:
// ENCODING
$data_array = array('id' => 1, 'name' => 'Juan Dela Cruz');
$serialized = serialize($data_array);
$encoded = base64_encode($serialized);
$this->load->model('data_mdl');
$this->data_mdl->_insert($encoded);
// DECODING
$encoded = //DO QUERY HERE TO GET ENCODED DATA;
$decoded = base64_decode($encoded);
$unserialized = unserialize($decoded);
CI model: CI模型:
public function _insert($data) {
return $this->db->insert($table_name, $data);
}
You can also use CI inbuilt encrypt library: SOLUTION 2 More details can be found here: https://www.formget.com/codeigniter-encrypt/ 您还可以使用CI内置加密库: 解决方案2可以在以下位置找到更多详细信息: https : //www.formget.com/codeigniter-encrypt/
I solve my problem by using $this->db->set()
function in my CI model. 我通过在CI模型中使用
$this->db->set()
函数解决了我的问题。
public function _insert($data) {
$this->db->set('name', 'ENCODE(\'' . $data['name'] . '\', \'mysecretkey\')', false);
unset($data['name']);
return $this->db->insert($table_name, $data);
}
I don't know if this is the right way but it solved my problem. 我不知道这是否是正确的方法,但是它解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.