Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: There is data in answer column. Thats how my data is going to be. I'm not able make it work. Can somebody please help me in this. Thanks already!!

like image 250
Abizz Avatar asked Dec 19 '25 00:12

Abizz


1 Answers

try this

echo htmlspecialchars($key->answer);

or

echo htmlentities($key->answer);

like image 151
Rabin Lama Dong Avatar answered Dec 20 '25 16:12

Rabin Lama Dong