php mysql_real_escape_string() 无法建立到服务器的链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4574665/
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 13:31:37  来源:igfitidea点击:

Mysql_real_escape_string() A link to the server could not be established

phpmysqlmysql-error-1045

提问by ganjan

I get this error when I try to use mysql_real_escape_string().

当我尝试使用 mysql_real_escape_string() 时出现此错误。

Access denied for user 'ODBC'@'localhost' (using password: NO)

I don't understand why I must be connected to the database to check if the values are OK to insert to MySQL.

我不明白为什么我必须连接到数据库来检查值是否可以插入 MySQL。

回答by John Parker

This is because mysql_real_escape_stringtakes into account the current character set of the connection. As such, it needs a connection. :-)

这是因为mysql_real_escape_string考虑了连接的当前字符集。因此,它需要一个连接。:-)

If you don't want to manually set up a connection in advance, you could set the various MySQL runtime defaultswithin your .ini file.

如果您不想提前手动设置连接,您可以在 .ini 文件中设置各种MySQL 运行时默认值

However, if you're using your own database class (or simply the manual commands), it's often worth establishing the connection early in the lifetime of your script.

但是,如果您使用自己的数据库类(或只是手动命令),通常值得在脚本生命周期的早期建立连接。

回答by Oswald

Because escaping depends on the character set on the server. See mysql_real_escape_string in the MySQL manual(the PHP function of the same name is just a wrapper around this C function).

因为转义取决于服务器上的字符集。参见MySQL手册中的mysql_real_escape_string(PHP同名函数只是对这个C函数的封装)。

回答by First Last

find and delete

查找和删除

mysql_close();

this function disconnect completely from MySQL server

此功能与 MySQL 服务器完全断开连接