I'm not sure what i could be missing, but currently with this working code I'm able to connect, query and return the results from the db table properly.
But how could show the results from the table with the correct encoding? since special chars like ç, á are being displayed sometimes like question mark or a square box.
Bellow, all the information that i have so far, but I believe definition must be set on the PDO because if i simply do an echo "ç -á";
on the page, the chars will be displayed properly.
Oracle 11 Table charset:
Query: SELECT value$ FROM sys.props$ WHERE name = 'NLS_CHARACTERSET' ;
Result: WE8MSWIN1252
php.ini file encoding settings:
default_charset = "UTF-8"
internal_encoding = "UTF-8"
input_encoding = "UTF-8"
output_encoding = "UTF-8"
Code example:
#
# PDO Connect example
#
$tns = "
(DESCRIPTION=
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
(CONNECT_DATA=
(SID=the_database)
)
)
";
$username = "x";
$password = "x";
try {
$conn = new PDO("oci:dbname=" . $tns, $username, $password );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
#
# Fetch results and build html table
#
$sql = "select * table";
$result = $conn->query($sql);
echo '<table>';
echo '<thead>';
echo '<tr>';
echo '<th>Info</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ($conn->query($sql) as $row) {
echo '<tr>';
echo '<td >' . $row['INFO'] . '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
Adding :charset=utf8
to the end of your DSN string should result in:
Since you said that a raw echo
of those special characters does appear correctly in your browser, then most likely your "viewer" settings (browser) are such that echoing the returned UTF-8 characters should succeed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.