简体   繁体   English

我应该选择使用MySQL中的哪种数据类型来使用PHP保存javascript代码?

[英]Which data type in mysql should i chose to save javascript code using PHP?

I'm making a website similar to jsfiddle, where the user can save their javascript codes and retrieve it back indented. 我正在建立一个类似于jsfiddle的网站,用户可以在其中保存其javascript代码并以缩进的方式检索它。 I don't know which DATA TYPE I should use to save the codes or if I should save them in text files. 我不知道应该使用哪种数据类型来保存代码,或者是否应该将其保存在文本文件中。 Also, when the data will be printed using php then how to indent it? 另外,当将使用php打印数据时,如何缩进呢?

You'd better save it in text files with a randomly chosen name and save the name into the database, or use the saved ID as name. 您最好将其保存在具有随机选择名称的文本文件中,然后将该名称保存到数据库中,或使用保存的ID作为名称。 Of course if you delete a row in the database you'll have to delete the file too. 当然,如果您在数据库中删除一行,则也必须删除该文件。

If you just want to save them in the database then choose TEXT or BLOB. 如果只想将它们保存在数据库中,则选择TEXT或BLOB。 VARCHARs are bad for large texts and if you don't know the lenght. 如果您不知道长度,VARCHAR对于大文本是不利的。

If you display/use content uploaded from users you have to carefully take care of people that try to exploit stuff . 如果您显示/使用从用户上传的内容,则必须小心照顾尝试利用东西的人

About indentation, if you display them in a textarea you shouldn't have any problems. 关于缩进,如果将它们显示在文本区域中,则不会有任何问题。

You could store the data in a varchar, but you shoulld probably pursue alternate storage possibilities such as storing them in individual *.js files. 您可以将数据存储在varchar中,但是您应该选择其他存储方式,例如将其存储在单独的* .js文件中。 As for indentation, all characters including indentation characters will be saved with the js. 至于缩进,包括缩进字符在内的所有字符都将与js保存在一起。

I would use BLOB it's quicker for retrieving than grabbing a filename out of the db then getting the file. 我将使用BLOB进行检索,这比从数据库中获取文件名然后获取文件要快。 Also, though the chances are slim if you have your site set up right, it's possible to execute the javascript files on the server opening it up to vulnerabilities if you're running node.js or something. 另外,尽管如果您正确设置网站的机会很小,但是如果您运行的是node.js或其他内容,则可以在服务器上执行javascript文件,使其面临漏洞。

You should use LONGTEXT type. 您应该使用LONGTEXT类型。 If you can guarantee that your data will be less than about 8KB, you can use VARCHAR or TEXT types ( Relevant MySQL documentation ). 如果可以保证数据少于8KB,则可以使用VARCHARTEXT类型( 相关的MySQL文档 )。

If there is possibility that text may contain some binary data, you may have to resort to BLOB or LONGBLOB types. 如果文本可能包含一些二进制数据,则可能必须诉诸BLOBLONGBLOB类型。

Regarding indentation: you can store tabs or spaces as well as newlines in your field and basically treat them as normal text files. 关于缩进:您可以在字段中存储制表符或空格以及换行符,并将其基本上视为普通文本文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM