Hi again

I'm still having problems with PHP/MySQL Warning messages, even when using mysql_real_escape_string().

The offending string is:

[25:64] And the true servants of the Gracious God are those who walk on the earth humbly and when the ignorant address them, they avoid them gracefully by saying, 'Peace!'

If I use this line on my WAMP server at home, I get now errors or warnings:

$quranVerse_1 = mysql_real_escape_string($_POST['quranVerse_1']);

However, when I use the same line on the hosted website, I get this series of errors/warnings:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Can't connect to MySQL server on 'localhost' (10061) in E:\domains\t\tarbiyyat.org\user\htdocs\php_newsletter\news_insert_record.php on line 7

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in E:\domains\t\tarbiyyat.org\user\htdocs\php_newsletter\news_insert_record.php on line 7

It's becoming so frustrating that we are contemplating going to a new hosting company.
So any help at all might prevent us going to all that trouble.

So the only thing I can see being the problem is the hosting companies PHP or MySQL setup must have something turned on or off?

Does anyone have a clue about this and how it can be fixed?

Regards
Terry

For mysql_real_escape_string to work you need to have an active connection to a mysql database. Make sure you are connected to the db and can query it before you call mysql_real_escape_string and you should not get a warning.

You can also try passing in the db connection variable as the second parameter to force it to use that open connection. ex: mysql_real_escape_string($var, $conn);

Hi Mazeroth

Yes I have an active connection and there isn't any problems with that. And obviously I also have an active connection on my WAMP server at home, which doesn't have this problem.

I am also just about to see what happens when I put a .htaccess file on the server with the command "php_flag magic_quotes_gpc on". I'm doing this because the host where the warnings are coming from doesn't have Magic Quotes turned on.

And I'm just doing this fiorst because I want to know if turning Magic Quotes On stops these warning messages.

But I will try your suggestion and let you know how I get on.

Well at least I know. putting in the .htaccess file didn't make any difference at all.

I've now tried putting my connection variable as the 2nd argument in mysql_real_escape_string, but immediately I received a different warning saying the $link (which is my connection variable) is undefined. Like below:

Notice: Undefined variable: link in E:\domains\t\tarbiyyat.org\user\htdocs\php_newsletter\news_insert_record.php on line 7
Warning: mysql_real_escape_string() expects parameter 2 to be resource, null given in E:\domains\t\tarbiyyat.org\user\htdocs\php_newsletter\news_insert_record.php on line 7

This is quickly becoming a bit of a nightmare.

It seems as if your db connection is on a different scope or just not working. Can you do a db query before that line of code to make sure your db connection is working as intended? Or var_dump your connection var to make sure there is something there?

Right. I've now managed to get it to work. And it was because the $link variable was null at the time of the mysql_real_escape_string() lines, so I moved them all down to just after the $link variable was populated.

So I'll do a bit more testing to make sure everything is working as it should.

Thanks again
Terry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.