我有一个包含一些永久文本的数据库表,我将输出到我的用户交互中。问题是我在查询数据库时需要更改这些永久文本答案的某些部分。
例如,这是我将给用户的回复之一:
I'm sorry, your answer is wrong. The correct answer is "ANSWER". Don't demoralize yourself and keep training!
但正如您所看到ANSWER
应该是我想在查询期间替换的数字或文本,因此我得到了正确的结果,作为示例结果:
I'm sorry, your answer is wrong. The correct answer is "Option 1". Don't demoralize yourself and keep training!
我在CodeIgniter(PHP Framework)中进行这些查询,这是我正在使用的代码样式:
public function mo_response_content($mo_response_content_id)
{
$this->db->select('*');
$this->db->where('id', $mo_response_content_id);
// Code to add for replacing part of the result
$query = $this->db->get('actions_mo_contents');
$row = $query->row();
if ($row)
{
return $row;
} else
{
return NULL;
}
}
如何在数据库查询期间替换ANSWER
字符串?
答案 0 :(得分:3)
使用MySQL REPLACE()
函数,如下所示:
$this->db->select("*, REPLACE(string_column, 'ANSWER', 'replaceTextHere') AS 'response'");