PHPlearner PHPlearner - 6 months ago 22
SQL Question

PHP won't echo "<" symbol from database

I'm trying learn PHP + mysql and when saved data to my database PHP wont echo lines that start with "<" symbol

mysql at screenshot, code under

$result2 = mysqli_query($con, "SELECT * FROM ta_pastat pastat left join ta_users tusers on pastat.kirjoittaja_ID = tusers.ID where pastat.kategoria_ID = 1");
while($row2 = mysqli_fetch_array($result2)){
//$row2 = preg_match('(^|>)(&gt;[^<]*)', "\\1<div class=\"unkfunc\">\\2</div>", $row2);

$style = "";
switch( substr($row2['Teksti'], 0, 1) ) {
case ">":
$style = "style=\"color:#789922\"";
break;
case "<":
$style = "style=\"color:#226199\"";
break;
}
//echo nl2br("<h4 $style>".$row2['Teksti']."</h4><br>".PHP_EOL);
echo nl2br("<br><h1>".$row2['Otsikko']."</h1><br><h4 $style>".$row2['Teksti']."</h4><hr>");

Answer

You will need to wrap it in htmlentities since the browser is interpreting it as HTML.

echo nl2br("<br><h1>".$row2['Otsikko']."</h1><br><h4 $style>".htmlentities($row2['Teksti'])."</h4><hr>");