Reputation: 216
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!!
Upvotes: 3
Views: 1460
Reputation: 38584
do like this
<?php foreach ($answeredQuestions as $key)
{
echo htmlspecialchars($key->answer);
}
Read more about PHP htmlspecialchars()
Function in W3school.com
Upvotes: 3
Reputation: 2476
try this
echo htmlspecialchars($key->answer);
or
echo htmlentities($key->answer);
Upvotes: 5