php - 使用 CodeIgniter 将数据库结果中的字符串替换为另一个字符串

标签 php mysql database codeigniter

我有一个数据库表,其中包含一些我将输出到我的用户交互的永久文本。问题是我需要在查询数据库时更改这些永久文本答案的某些部分。

例如,这是我将给用户的响应之一:

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/

相关文章:

php - 我的 IF 语句有问题

php - 使用 nginx 在 linux 服务器上部署 Laravel 应用程序出现 404 错误

php - 将鼠标悬停在图像上时如何使对象出现

mysql - 总结过去 7 天回顾窗口的数据

mysql - 为什么 mysql 中的日期会出现此错误消息?

ruby-on-rails - 如何从 Rails 中的 ActiveRecord 查询中删除异常值

mysql - 使用连接进行限制 X,Y MySQL 查询运行速度更快

php - Preg_Match 精确单词并从字符串文本中输出单词

javascript - ajax 文本变量传递 0 或 NaN

c# mysql - 使用 mySqlParameter 更新字段值以包含\r\n