Text messed up when returned from DB

czechmate1976 used Ask the Experts™
When I submit text with apostrophies and speech marks in the text area.. when the text returns from the db totally messed up.

the submitted text is dealt as follows:
$content = htmlentities(mysql_prep($_POST['content']));

where mysql_prep is a function => below

the returned text is displayed the following way:


the database field is set to text, utf8-general-ci

Will be very greatful if someone help me sort out this problem - I can't even think straight after a long weekend working ... Thanks a lot for all your tips

function mysql_prep( $value ) { //parameter passed by the function caller - variable from a form
	$magic_quotes_active = get_magic_quotes_gpc(); // this function has been deprecated in PHP 5.3.0 and removed from PHP6.0.0
	$new_enough_php = function_exists( "mysql_real_escape_string"); // Tests if a function in brackets exists (returns TRUE or FALSE; it is to find out if PHP version is higher than v4.3
	if( $new_enough_php ) { // PHP is of v4.3.0 or higher
	// undo any magic quote effects so mysql real_escape_string function can do it
		if( $magic_quotes_active ) { $value = stripslashes( $value ); }
		$value = mysql_real_escape_string( $value );
	} else { // PHP versions older than v4.3.0
	//if magic quotes aren't already on then add slashes manually
		if( !$magic_quotes_active ) { $value = stripslashes( $value ); }
		// if magic quotes are active, then the slashes already exists
	return $value;

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Show us some input and the output, please.


ok, here is a sample... thanks a lot - Not sure if it has much to do with that - but the text is copied from a website with these speechmarks etc.

The requirements for a mobile phone or 3G card are very important for the dayto-day communication for Agip KCO , FGE are able to provide a connection and hardware on the same day or within 2 days which is really good service

âýýThe requirements for a mobile phone or 3G card are very important for the dayâýýto-day communication for Agip KCO , FGE are able to provide a connection and hardware on the same day or within 2 days which is really good serviceâýý
It seems to me the problem is not with your function unless you are using php older than 4.3. And in this case you may need to complete processing of
if( !$magic_quotes_active ) { $value = stripslashes( $value ); }
if( !$magic_quotes_active ) { $value = stripslashes( $value ); $value=str_replace("'", "\'", $value;}

The problem is with your data and may be the keyboard layout, because this char () is strange in this context.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.


Will check this out. thanks a lot!!1 Will let you guys know what I get from it, well after a bit of sleep :-)
Sounds like an encoding issue, I had similar problems because the database was set to something like ISO-8859 and I needed it to be UTF-8. Try using one of the appropriate php functions to convert the character encoding before displaying the text:
First send the charset utf-8 in the HTTP header of the page containing the form, so the web browser knows to return UTF-8 encoded response. Or you can add the charset: utf-8 to the enctype of the <form> itself.

Second, make sure your connection to the database is setup to use UTF-8.
// Tell the web browser UTF-8 is sent (and expected for response)
header('Content-Type: text/html; charset=utf-8');
<!-- or set the charset in the <form> tag -->
<form enctype="application/x-www-form-urlencoded; charset=UTF-8">
// To be set up once per connection in the script
mysql_query('SET CHARACTER SET utf8');

Open in new window


Thank you guys

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial