Reputation: 11
I am building an app with Apache cordova for the support team for my company and everything was ok when I was using a test database in UTF8 was working.
Then when I was implement the real db I notice it was encoded with win-1252.
The problem is, even the db is with win-1252 we have many rows using special caracters like "ç" and "~" and "´" and "`" and with that when I am running the php all rows in the tables in my db will not show becasue of that.
Keep in mind I cann't convert the db to utf8.
ps:The solution I see is go to each row and remove that caracters but isn't a good solution(about 20,000 rows)
........................
PHP file:
header("Access-Control-Allow-Origin: *");
$dbconn = pg_connect("host=localhost dbname=bdgestclientes2
user=postgres password=postgres")
or die('Could not connect: ' . pg_last_error());
$data=array();
$q=pg_query($dbconn,"SELECT * FROM clientes WHERE idcliente = 3");
$row=pg_fetch_object($q)){$data[]=$row};
echo json_encode($data);
Upvotes: 0
Views: 852
Reputation: 11
I just needed to add a line in php to encode to unicode so I could use the data and display the way it is
pg_set_client_encoding($dbconn, "UNICODE");
Upvotes: 1
Reputation: 28722
I have a similar issue, where I cannot modify the database setup, but I use php's html entity encode to work around:
I removed the html key elements from the native htmlentities because I work with wysiwyg editors and need to keep the content like that. If you have no such limitations you can just use htmlentities on the string.
function makeFriendly($string)
$list = get_html_translation_table(HTML_ENTITIES);
unset($list['"']);
unset($list['\'']);
unset($list['<']);
unset($list['>']);
unset($list['&']);
$search = array_keys($list);
$replace = array_values($list);
$search = array_map('utf8_encode', $search);
str_replace($replace, $search, $string);
}
If I need the actual characters I can always call html_entity_decode on the database string to get the 'real' string.
Upvotes: 0
Reputation: 246268
That shouldn't be a problem at all.
Windows-1252 supports “ç” (code point 0xE7), “~” (code point 0x7E), “`” (code point 0x60) and “´” (code point 0xB4).
PostgreSQL will automatically convert the characters to the database encoding.
You will get problems if you want to store characters that do not occur in Windows-1252, like “Σ”. In that case, the correct solution is to use a database with a different encoding (UTF8).
If you cannot do that, you'll have to store the strings as binary objects (data type bytea
) and handle encoding in your application. That will only work well if you don't need to process these functions in the database (e.g., use an index for case insensitive search).
Upvotes: 0