我有一个数据库表,其中包含一些我将输出到我的用户交互的永久文本。问题是我需要在查询数据库时更改这些永久文本答案的某些部分。
例如,这是我将给用户的响应之一:
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 框架)中执行这些查询,这是我正在使用的代码风格:
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
字符串?
最佳答案
使用 MySQL REPLACE()
功能如下:
$this->db->select("*, REPLACE(string_column, 'ANSWER', 'replaceTextHere') AS 'response'");
关于php - 使用 CodeIgniter 将数据库结果中的字符串替换为另一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18099146/