在CodeIgniter 2应用程序中,当我执行密码恢复过程时,代码会触发php通知"非法偏移类型"。我发现这是因为它提交整个CI_DB_mysql_driver对象到活动记录类的_where函数,而通常它应该只是提交一个数据库字段数组(作为数组键)和各自的值(作为数组值)。
步骤如下:
1。用户模型中更新用户信息的函数
function update($data,$where,$value){
$filter = $this->db->where($where,$value);
$this->db->update('user',$data,$filter);
}
2。DB_active_rec.php中处理'where'部分mysql查询的函数
public function where($key, $value = NULL, $escape = TRUE){
return $this->_where($key, $value, 'AND ', $escape);
}
protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
{
if ( ! is_array($key))
{
$newkey = array($key => $value);
$key = $newkey;
}
// If the escape value was not set will will base it on the global setting
if ( ! is_bool($escape))
{
$escape = $this->_protect_identifiers;
}
foreach ($key as $k => $v)
{
$prefix = (count($this->ar_where) == 0 AND count($this->ar_cache_where) == 0) ? '' : $type;
if (is_null($v) && ! $this->_has_operator($k))
{
// value appears not to have been set, assign the test to IS NULL
$k .= ' IS NULL';
}
if ( ! is_null($v))
{
if ($escape === TRUE)
{
$k = $this->_protect_identifiers($k, FALSE, $escape);
$v = ' '.$this->escape($v);
}
if ( ! $this->_has_operator($k))
{
$k .= ' = ';
}
}
else
{
$k = $this->_protect_identifiers($k, FALSE, $escape);
}
$this->ar_where[] = $prefix.$k.$v;
if ($this->ar_caching === TRUE)
{
$this->ar_cache_where[] = $prefix.$k.$v;
$this->ar_cache_exists[] = 'where';
}
}
return $this;
}
它返回php通知"非法偏移类型"。下面的对象似乎是作为$key参数传递给_where函数的:
CI_DB_mysql_driver Object
(
[dbdriver] => mysql
[_escape_char] => `
[_like_escape_str] =>
[_like_escape_chr] =>
[delete_hack] => 1
[_count_string] => SELECT COUNT(*) AS
[_random_keyword] => RAND()
[use_set_names] =>
[ar_select] => Array
(
)
[ar_distinct] =>
[ar_from] => Array
(
)
[ar_join] => Array
(
)
[ar_where] => Array
(
[0] => `u_email` = 'test@tester.com'
)
[ar_like] => Array
(
)
[ar_groupby] => Array
(
)
[ar_having] => Array
(
)
[ar_keys] => Array
(
)
[ar_limit] =>
[ar_offset] =>
[ar_order] =>
[ar_orderby] => Array
(
)
[ar_set] => Array
(
[`u_pass`] => 'Vm4IOQZpBiIFJw=='
)
[ar_wherein] => Array
(
)
[ar_aliased_tables] => Array
(
)
[ar_store_array] => Array
(
)
[ar_caching] =>
[ar_cache_exists] => Array
(
)
[ar_cache_select] => Array
(
)
[ar_cache_from] => Array
(
)
[ar_cache_join] => Array
(
)
[ar_cache_where] => Array
(
)
[ar_cache_like] => Array
(
)
[ar_cache_groupby] => Array
(
)
[ar_cache_having] => Array
(
)
[ar_cache_orderby] => Array
(
)
[ar_cache_set] => Array
(
)
[ar_no_escape] => Array
(
)
[ar_cache_no_escape] => Array
(
)
[username] => root
[password] => temproot
[hostname] => localhost
[database] => oceantailer
[dbprefix] =>
[char_set] => utf8
[dbcollat] => utf8_general_ci
[autoinit] => 1
[swap_pre] =>
[port] =>
[pconnect] => 1
[conn_id] => Resource id #29
[result_id] => Resource id #55
[db_debug] => 1
[benchmark] => 5.8174133300781E-5
[query_count] => 1
[bind_marker] => ?
[save_queries] => 1
[queries] => Array
(
[0] => SELECT *
FROM (`user`)
WHERE `u_email` = 'test@tester.com'
)
[query_times] => Array
(
[0] => 5.8174133300781E-5
)
[data_cache] => Array
(
)
[trans_enabled] => 1
[trans_strict] => 1
[_trans_depth] => 0
[_trans_status] => 1
[cache_on] =>
[cachedir] =>
[cache_autodel] =>
[CACHE] =>
[_protect_identifiers] => 1
[_reserved_identifiers] => Array
(
[0] => *
)
[stmt_id] =>
[curs_id] =>
[limit_used] =>
[stricton] =>
)
如何防止这个php提示?
这个成功了:
在'users'模型中:
function update($data,$where,$value){
$this->db->where($where,$value);
$this->db->update('user',$data);
}
在DB__active_rec.php :
protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) {
if ( ! is_array($key)) {
$key = array($key => $value);
}
...