Tina Tina - 2 years ago 85
PHP Question

Displaying Quotation Mark Characters while retreiving from in Database to php

I am storing large amounts of text in a database. The problem is that when I try to store quotation marks (‘ ’ “ ” not " ') they store in the database fine although do not show when I show the text from the database.!

i have tried changing collation in sql to utf8_unicode_ci,still not changing

(‘ ’ “ ” not " ') are replaced by a questain mark within a square black box

Column Type Collation Attributes Null Default Extra Action

2 qr_topics text utf8_unicode_ci No None

Answer Source

You need to run your database return through htmlentities() before displaying it!

$results = $pdo->fetchAll();

foreach($results as $result) {
    echo htmlentities($result['fieldQuotesAndOtherGoodies']);

Not only is this good practice (especially when working with different charsets or with brackets, < and >, which you have to encode because they will otherwise be interpreted as HTML tags), this is also a very important for security. Your site can be vulnerable to XSS (and therefore CSRF) attacks without the encoding of user data retrieved from the database.

Just for reference: the solution was to simply add a meta tag that ensured the HTML delivered was interpreted as UTF-8 by the browser:

<meta charset="UTF-8" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download