0

I have been working on retrieving data from a database, I have been having trouble getting the accented characters to show (they appear as ? and I have tried many things to try and get this to work).

I just tried putting the data into a file using file_put_contents which means the issue might be with the terminal itself and not with the character encoding, unless I am wrong? I then tried file_get_contents to read the file with the correct accented characters, and it still shows as ? in the terminal

Does anyone have any idea how I could get to show the data in the terminal with accented characters included? If I try to echo a simple é it shows up perfectly.

Thanks!

My code:

<?php

ini_set('default_charset','utf-8');

header('Content-Type: text/html; charset=utf-8');

$username = "username";
$password = "password?";
$dsn = "dsn";

$connection = odbc_connect($dsn, $username, $password, SQL_CUR_USE_ODBC);

$sql = "SELECT \"Insert ID\", \"Date Created\", \"Date Modified\", Description FROM Insert";

$res = odbc_exec($connection,$sql);

$x=0;

while ($row = odbc_fetch_array($res)){

$x++;


   $values= ($x . ": " . " Insert ID:". $row['Insert ID'] . " Date Created: " . $row['Date Created'] . " Date Modified:" . $row['Date Modified'] . " Dscription:" . $row['Description'] . "\n");




   print($values);
}

odbc_free_result($res);

odbc_close($connection);
Exn
  • 761
  • 3
  • 9
  • 19

1 Answers1

1

Your PHP file is most likely saved as ISO Latin 1 or Mac OS Roman. Change it to UTF-8 and it should work. I just tried it on my Mac and it output 'special' characters no problem:

<?php
//test.php
$data = 'é';

file_put_contents('./test.txt', $data);

$output = file_get_contents('./test.txt');

echo $output.PHP_EOL;

Run the script in Terminal.app:

 $ php test.php //outputs 'é'
cOle2
  • 4,725
  • 1
  • 24
  • 26
  • Your script works no problem, it will display é properly. However I still have issues with my own script, I made sure it was UTF-8 too. – Exn Jul 02 '14 at 14:55
  • i just realized the text file was in Mac OS Roman, so now when I import the text file it does show accented characters, you were right. But the thing is I don't want to import the data into a text file and then import it into m terminal again, any idea why it won't show accented characters right away? – Exn Jul 02 '14 at 17:03
  • @Exn: First guess would be not all your files are saved as UTF-8, or your DB columns are not UTF-8. Hard to say without seeing some code... – cOle2 Jul 02 '14 at 17:08
  • I added code in my question, I only have access to the database from the odbc connection. – Exn Jul 02 '14 at 17:24
  • @Exn: Do you know which encoding the database columns use then? Have you tried `utf8_decode($row['Description'])` (assuming thats where the accents characters are) ? – cOle2 Jul 02 '14 at 17:37
  • I'm not sure, I tried utf8_decode, I get the same problem. – Exn Jul 02 '14 at 17:52
  • @Exn: The only other thing I can think of is trying `iconv("Windows-1256", "UTF-8", $row['Description'])`. Not knowing which encoding the data is in makes it a bit tricky. – cOle2 Jul 02 '14 at 17:58
  • I got it to work by using "macintosh" instead of "Windows-1256. Thanks for the help! – Exn Jul 02 '14 at 18:05