mysql_real_escape_string($text) is not working on live server

525 views Asked by At

I am using this mysql command to remove extra character from string. Command is below mysql_real_escape_string($text);

The above command is working completely in local server but it is not working on live server, it gives error like "mysql connection not open".

2

There are 2 answers

0
Kishan Vadaliya On BEST ANSWER

mysql_real_escape_string($text) command need connection with database. So, you need to open connection before this command and then close the connection after it.

your code is:

mysql_connect('127.16.1.11','admin','password');
mysql_real_escape_string($text);
mysql_close();
0
nikhil On

You need to have connected to a database for using this function. Without connecting to a db its impossible to use mysql_real_escape_string() Instead you can use this function

function escape($value)
{
    $search = array("\\",  "\x00", "\n",  "\r",  "'",  '"', "\x1a");
    $replace = array("\\\\","\\0","\\n", "\\r", "\'", '\"', "\\Z");

    return str_replace($search, $replace, $value);
}

Now if u do escape($text); you will have same result