php 我应该使用什么数据类型在 mysql 数据库中存储电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15631919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:39:12  来源:igfitidea点击:

what data type should i use to store email in mysql database?

phpmysql

提问by Nettai manoj

I have a small website and I want to store the email of my contacts in a MySQL database. I am using PHP and MySQL. Each time I try, it gives me following error. However when I remove the @and .(dot) it works fine. I really need a help on this one, please tell me what mistake I am doing and it would be very helpful if the code is given.

我有一个小网站,我想将我的联系人的电子邮件存储在 MySQL 数据库中。我正在使用 PHP 和 MySQL。每次我尝试时,它都会给我以下错误。但是,当我删除@.(点)时,它工作正常。我真的需要这方面的帮助,请告诉我我在做什么错误,如果给出代码会非常有帮助。

I already used VARCHARonly. It keeps showing up the error below:

我已经VARCHAR只用了。它一直显示以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(3' at line 1

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在“@gmail.com(id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(3' at line 1)”附近使用正确的语法

回答by Hardik

you can use vachardatatype with 1-255length.

您可以使用vachar具有1-255长度的数据类型。

And for more information if your value's length is more then 255then use textdatatype

有关更多信息,如果您的值的长度大于255然后使用text数据类型

回答by Fadi Obaji

Use varchar and wrap the email in quotes so it can be treated as a string like so:

使用 varchar 并将电子邮件括在引号中,以便将其视为字符串,如下所示:

'[email protected]'

'电子邮件@gmail.com'

That's it bro.

就是这样哥

回答by Yolknet

Besides the error with the @, you still have a query that won't work: gmail.com( id INT.... shouldn't that be gmail.com', ( id INT....?

除了@ 的错误之外,您还有一个不起作用的查询:gmail.com( id INT .... 不应该是 gmail.com', ( id INT ....?

The fact that your database can't handle a @ can be caused by the characterset of the database. Is it UTF-8 Unicode (utf8)? And what is the type of the record where you want to store the mail?

您的数据库无法处理 @ 的事实可能是由数据库的字符集引起的。它是 UTF-8 Unicode (utf8) 吗?您要存储邮件的记录类型是什么?

回答by Jhonathan H.

a varchar(50) would be enough for storing email..

a varchar(50) 足以存储电子邮件..

Then in your query all the values to insert must wrapped by ''or ""

然后在您的查询中,所有要插入的值必须由''""

e.g

例如

$sql='INSERT INTO TABLE1 VALUES("Value1","[email protected]","VALUES3")'