Php 更新查询未更新相应的行
我在Codeigniter中遇到了一个奇怪的问题。我正在运行更新行的查询:Php 更新查询未更新相应的行,php,mysql,codeigniter,Php,Mysql,Codeigniter,我在Codeigniter中遇到了一个奇怪的问题。我正在运行更新行的查询: function set_counter($key, $value){ echo "$key : $value => "; $this->db->query("UPDATE counter SET counter.`value` = ? WHERE counter.`key` = ?", array($value, $key)); return $this->db->
function set_counter($key, $value){
echo "$key : $value => ";
$this->db->query("UPDATE counter SET counter.`value` = ? WHERE counter.`key` = ?", array($value, $key));
return $this->db->affected_rows();
}
Benchmark显示调用此函数并运行查询:
UPDATE counter SET counter.`value` = 1 WHERE counter.`key` = 'left_banner_start_id'
函数返回“1”,这意味着更新了1行
但实际上,该行在数据库中不会得到更新,值列始终为“0”
我做错了什么,有什么解决办法吗
编辑:
问题变得更加奇怪,当我退出函数时,行实际上会得到更新,否则它不会:
function set_counter($key, $value){
echo "$key : $value - ";
$this->db->query("UPDATE counter SET counter.`value` = ? WHERE counter.`key` = ?", array($value, $key));
//print_r($this->db->affected_rows());
exit();
}
编辑:
好的,如果我在system/core/Output.php中退出执行,它就会工作:
// --------------------------------------------------------------------
// Does the controller contain a function named _output()?
// If so send the output there. Otherwise, echo it.
if (method_exists($CI, '_output'))
{
$CI->_output($output);
}
else
{
exit();
echo $output; // Send it to the browser!
}
但如果我在回显$output后退出它,则不起作用:
// --------------------------------------------------------------------
// Does the controller contain a function named _output()?
// If so send the output there. Otherwise, echo it.
if (method_exists($CI, '_output'))
{
$CI->_output($output);
}
else
{
echo $output; // Send it to the browser!
exit();
}
编辑:
在我看来,有一条线阻止了它:
<img src="<?php echo base_url().$recent_ad['photo_thumb']; ?>" width="28" height="27" />
从另一个角度来看:
<img src="<?php echo base_url().$featured_agent['photo_medium']; ?>" width="84" height="84" />
这里photo_thumb和photo_medium是数据库中另一个表中的列,它们的值为空。我填写了所有的栏目,现在一切正常
但是为什么呢?原因是什么?您是否尝试切换活动记录语法的代码
function set_counter($key, $value){
$this->db->where('key', $key)->update('counter', array('value' => $value));
return $this->db->affected_rows();
}
是否在函数中打印正确的值?是。一切都很好。甚至codeigniter也显示查询运行时没有错误,并显示数据库中有1行已更新。但实际上没有任何东西得到更新;没有打印值我只是在用那个语句调试。它正在打印我在问题中已经告诉过的值。然后,也许你可以告诉我们从哪里调用这个方法。问题是,您面临的问题在CI引擎上不会有任何影响,因为这是一个常见的更新查询。很可能是无效值。实际上,您说过photo_thumb和photo_medium是由其他表检索的数据。您是否可能正在进行内部联接,将这些值关联起来,并通过同一查询获取counter.key值?因为那样的话,如果你在做一个带有空值的内部操作,就没有键了。这正是困扰我的问题。photo_thumb和photo_medium是完全不相关的列。查询不必对这些列甚至表进行任何处理。没有,$key和$value是有效字段,没有使用内部联接,我告诉codeigniter确实显示查询已成功运行,但数据未更新这是由基准测试获得的:UPDATE counter SET counter.value=1,其中counter.key='left\u banner\u start\u id',因此查询中没有错误。