简体   繁体   中英

Cannot fetch some special characters from mysql table in codeigniter

I have a issue while fetching some data from the mysql table whose data type is "text". What i have is a portal for instructor and student. Student will ask question and the instructor can answer them by text or put some code in that. Say If a student has asked a demo code for php echo so the instructor can put the code

<?php echo "Hello World!" ?>

The issue is this answer is saved in database but when i'm trying to fetch, I can't fetch the result. The problem is if I put '<' & '?' together the code is not fetched but when I put space in between them then it is fetched. I have tried many things like my charset in config of database

'char_set' => 'UTF8',
'dbcollat' => 'utf8_general_ci',

Then my meta

<meta charset=utf-8"/>

I have tried everything but i cant really fetch it. My model code to fetch the answer is

public function getAnswer($questionId)
{
    $this->db->select('answer');
    $this->db->from('tbl_que_ans as qa');
    $this->db->where('qa.questionId', $questionId);
    $query = $this->db->get();
    $query_result = $query->row();
    return $query_result;
}

While displaying it in the view i'm using foreach

<?php foreach ($answeredQuestions as $key) {
      echo $key->answer;
?>

This how my datatable looks: 答案栏中有数据。多数民众赞成我的数据如何。 I'm not able make it work. Can somebody please help me in this. Thanks already!!

try this

echo htmlspecialchars($key->answer);

or

echo htmlentities($key->answer);

do like this

<?php foreach ($answeredQuestions as $key) 
{
      echo htmlspecialchars($key->answer);
}

Read more about PHP htmlspecialchars() Function in W3school.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM