【问题标题】:Replacing a string with another in the database result using CodeIgniter使用 CodeIgniter 将数据库结果中的字符串替换为另一个字符串
【发布时间】:2013-08-07 09:05:43
【问题描述】:

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

例如,这是我将给用户的回复之一:

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字符串?

【问题讨论】:

    标签: php mysql database codeigniter


    【解决方案1】:

    使用MySQLREPLACE()函数如下:

    $this->db->select("*, REPLACE(string_column, 'ANSWER', 'replaceTextHere') AS 'response'");
    

    【讨论】:

    • Mamnoon,你会不会碰巧知道如何将其格式化为对 codeigniter 友好的方式(类似于上面的函数)?
    • @Freelancing_Best_Choice 实际上 CI Active 记录中并没有完全支持 SQL 函数。这种方式没问题。但是,您可以使用query(); 方法手动查询数据库。
    • 我希望它存在,因为我不想更改模型中的当前格式,但无论如何它工作正常。 باز هم ممنون
    • 很棒的概念,亲爱的,我这一天一直在想我是如何解决这个问题的,你做到了,爱你
    猜你喜欢
    • 2011-12-21
    • 1970-01-01
    • 2012-07-16
    • 2019-05-14
    • 2019-12-03
    • 2013-12-03
    • 1970-01-01
    • 2021-06-15
    • 2018-02-03
    相关资源
    最近更新 更多