0

I have a simple database where I have trip descriptions for a trip. In the database, the trip descriptions include apostrophes and enters/returns, it is type: text.

I want to retrieve and echo out all of the trip descriptions (there are a lot of them) onto a website, however the apostrophes, hyphens, and other special characters are turning into � and the enters/returns just aren't there.

For example, the following text in the database:

We'll arrive in New Georgia to paddle around the famous Marovo Lagoon - the world's largest salt water lagoon - for seven days.

We will return to...

gets displayed on the webpage as

We'll arrive in New Georgia to paddle around the famous Marovo Lagoon � the world�s largest salt water lagoon � for seven days. We will return to...

The "We will return to..." is not in a new paragraph, and the apostrophe and two hyphens are �

Here is my php code:

$tripResult = mysql_query("SELECT * FROM trips");
while($row = mysql_fetch_array($tripResult)){
    echo $row['tripDescription'];
}

I've tried replacing the echo line with

echo htmlspecialchars($row['tripDescription'], ENT_QUOTES);

But this displays no text at all.

EDIT: I have also tried checking that I have a charset meta tag, changing the database collation to utf8mb4_general_ci (the closest one to utf8mb4 I could find in the dropdown list), and replacing the echo with:

echo mysql_set_charset($row["tripDescription"]);

However it still displays no text at all. How can I make this text look normal?

Peter
  • 33
  • 6
  • 1
    Possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – FirstOne Jun 30 '16 at 00:58
  • [nl2br](http://php.net/manual/en/function.nl2br.php) – FirstOne Jun 30 '16 at 01:01
  • When I use the nl2br function, that displays the 'enters' onto the page which is good, but what do you mean by duplicate of UTF-8 all the way through? On phpmyadmin, I see that under 'collation' the table is latin1_swedish_ci which was the default so I just left it like that. I tried changing it to some of the ones that begin with utf-8, but that made no difference – Peter Jul 02 '16 at 09:47
  • @FirstOne read above, nl2br solved half of the problem, but I'm still unsure what you mean by UTF-8 all the way through/how I could get rid of the � – Peter Jul 03 '16 at 08:00
  • That link might help you figure out how to fix that char (`�`) problem. It provides a bunch of information on how to set the database, the database connection and the page as UTF-8. (See, from database proprieties to data printing, so 'all the way through'). – FirstOne Jul 03 '16 at 12:45
  • @FirstOne sorry, I've very new to this. I tried to understand that page but no luck. I've made sure I've declared charset in a meta tag, and I've changed the collation in the database tables to utf8mb4_general_ci (that looked like the closest one to utf8mb4), and I've tried to echo with `echo mysql_set_charset($row["tripDescription"])`, but it just disappears... – Peter Jul 05 '16 at 10:54
  • you don't `echo` it like that. After you set any needed configuration to `utf8`-ish, you just echo stuff normally... You won't have to call that for every echo – FirstOne Jul 05 '16 at 10:58
  • @FirstOne so I've set the tables in the database to utf8mb4_unicode_ci, and I echo the lines just like `echo $row['tripDescription'];` but this still shows the �. I'm kind of stuck on what to do. Is it something to do with the meta tag? `` – Peter Jul 05 '16 at 23:05
  • It's possible, but tbh, I don't even know anymore xD. Try adding it if you haven't already done that. I'm out of options... – FirstOne Jul 06 '16 at 01:19
  • haha yeah, not sure what else to do here, thanks for the help anyways :) – Peter Jul 06 '16 at 04:01
  • One last thing: try changing to other encodings.. (that might screw up other things, but just try it) – FirstOne Jul 06 '16 at 10:16
  • Hmm yeah, I've tried that and it seems to just do nothing. I don't think I'm doing this right :/ – Peter Jul 07 '16 at 00:47

1 Answers1

0

Insert your data using addslashes — Quote string with slashes and get using stripslashes.

$tripResult = mysql_query("SELECT * FROM trips");
while($row = mysql_fetch_array($tripResult)){
    echo stripslashes($row['tripDescription']);
}

Use stripslashes — Un-quotes a quoted string

Hamza Zafeer
  • 2,360
  • 13
  • 30
  • 42
  • Is there a way to output it "nicely" if the text in the database was not inserted using addslashes? I.e. just in plaintext? – Peter Jun 30 '16 at 02:26
  • read above, what if the text in the database has to be just regular English? – Peter Jul 03 '16 at 07:59