使用php显示包含来自数据库的html标签的内容
问题描述:
Table name : Post, column name : content say, below data along with the html tag and style is stored in content column
<h1 style="color:red">test</h1>
I'm using below php code to display it
<?php
$sql = "SELECT content FROM post where id = 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<?php echo htmlspecialchars($row['content'], ENT_QUOTES); ?>
<?php
}
}
else {
echo "0 rasdesults";
}
$conn->close();
?>
expectation : test in red color but output : only "test"
答
Please replace you echo syntaxt
<?php echo htmlspecialchars($row['content'], ENT_QUOTES); ?>
with below line
<?php echo htmlspecialchars_decode($row['content']);?>
Hope this will help you!
Thanks & regards.
Shishil Patel