Question

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

Was it helpful?

Solution

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();

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top