tags:

views:

46

answers:

1

Hi, I am getting the error; Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Why do I get this error? The mysql_real_escape_string() works on all of my pages apart from one? Is it something to do with MySQL being on a different server to the PHP server - if so, how do I fix it?

    $fname = $_POST['fname'];
$fname = stripslashes($fname);
$fname = mysql_real_escape_string($fname);

Thanks Tom

+3  A: 

This is because you never call mysql_connect() before the use of mysql_real_escape_string().

In order to use mysql_real_escape_string(), PHP must be connected to the database. In order to connect to the database, you must use mysql_connect().

Chacha102
Out of curiosity, why does it need to call the database to execute that function?
Spencer Ruport
The PHP code invokes the MySQL's library function to escape the strings, so in order for the code to invoke the library function, it must be connected first.
Anthony Forloney
It calls a MySQL library function to do the escaping. It is not done in PHP directly.
Sasha
@Spencer Good question. It's because the escaping depends on the character set used by the server ( see http://dev.mysql.com/doc/refman/5.1/en/mysql-real-escape-string.html )
therefromhere