mysql存储HTML需要注意什么 [英] What precautions should be taken for storing HTML in mysql

查看:69
本文介绍了mysql存储HTML需要注意什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何在数据库中作为HTML标签保存?

我将通过我的 php 程序的管理页面存储 HTML 代码.我在 PDO 中使用准备好的语句进行存储.但在此之前,我需要使用 htmlspecialchars() 或 htmlentities() 吗?

I will store HTML code through admin page of my php program. I use prepared statements in PDO for storing. But before that, do i need to use htmlspecialchars() or htmlentities() ?

或者通过使用准备好的语句存储 html 会正常工作吗?以后有什么开销吗?

Or by storing html using prepared statement will work fine ? Any overheads later ?

稍后此 html 用于在 HTML 页面内显示为内容,并且此数据库中的内容应呈现为 HTML 本身.

Later this html is used to display as content inside a HTML page and this content from database should render as HTML itself.

推荐答案

实际上,在您的情况下,无需采取任何预防措施.

不需要 htmlspecialchars() 因为您想显示 html 并且不需要使用 htmlentities() 如果您网站的字符集等于您在数据库中使用的一个.此外,您不必自己转义字符串,因为准备好的语句会解决这个问题.

There is no need for htmlspecialchars() as you want to display html and no need to use htmlentities() if the character set of your site is equal to the one you use in your database. Also you don't have to escape the string on your own as prepared statements will take care of that.

然而,htmlentities() 不会造成任何伤害,但使用它只会浪费性能.处理字符集的最简单方法是简单地使用 UTF-8 来避免任何冲突.

However, htmlentities() will not cause any harm but using it is just waste of performance. The easiest way to deal with the character set is to simply use UTF-8 to avoid any conflicts.

这篇关于mysql存储HTML需要注意什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆