Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - How to echo a code as a text

Now I got a query that contains HTML code and when I select it in php it makes the code,

$query = mysql_query("SELECT * FROM `table`")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$code = $arr ['code'];
echo $code;
}

and in that query there is this code <a href="http://google.com">Click Here</a>

when it echo it shows me Click Here, But I want it to sows me the code.

So how can I do that in PHP.

Thanks
Klaus

like image 787
Klaus Jasper Avatar asked Dec 15 '22 09:12

Klaus Jasper


2 Answers

Use htmlspecialchars() to prevent HTML elements from being interpreted by the browser.

$query = mysql_query("SELECT * FROM `table`")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
    $num = mysql_num_rows($query);
    $code = $arr ['code'];
    echo htmlspecialchars($code);
}
like image 57
Cobra_Fast Avatar answered Jan 12 '23 00:01

Cobra_Fast


Just encode the output with htmlspecialchars

$query = mysql_query("SELECT * FROM `table`")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$code = $arr ['code'];
echo htmlspecialchars($code);
}
like image 20
Musa Avatar answered Jan 11 '23 23:01

Musa