4

I get this error when I try to use 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.

Peter O.
  • 28,965
  • 14
  • 72
  • 87
ganjan
  • 6,443
  • 22
  • 74
  • 126
  • 2
    it gets quite explained on this function's manual page – Your Common Sense Jan 01 '11 at 16:23
  • @Col.Shrapnel: Just had a look. The manual does mention the warning and cause, but doesn't give a very sensible explanation - in particular about the charset issue. – mario Jan 01 '11 at 16:25
  • http://dev.mysql.com/doc/refman/5.0/en/charset-literal.html + http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html – ajreal Jan 01 '11 at 16:30

3 Answers3

13

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

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

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.

John Parker
  • 52,372
  • 11
  • 124
  • 125
  • however, this feature almost never works, as almost noone using mysql_set_charset() function. – Your Common Sense Jan 01 '11 at 16:25
  • @Col. Shrapnel - Sad, yet probably true. There's a definite lack of knowledge of connection, character set and indeed table collation encodings. Still, such is life. :-) – John Parker Jan 01 '11 at 16:35
  • Well it doesn't matter for the 99% encodings anyway. Also, everyone is going (or, at least, *says* that they are going) towards prepared statements, so, it doesn't matter again – Your Common Sense Jan 01 '11 at 16:40
  • 1
    @Col. Shrapnel - Of course, I'm sure simply *everyone* will be using PDO by the end of next mumble mumble mumble. :-) – John Parker Jan 01 '11 at 16:42
1

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).

Oswald
  • 29,474
  • 3
  • 39
  • 67
-3

find and delete

mysql_close();

this function disconnect completely from MySQL server

First Last
  • 123
  • 2
  • 9