Solution to the garbled code in PHP connection to Oracle: 1. Get the character set of Oracle by running the "select * from V$NLS_PARAMETERS;" command; 2. Modify the variable "NLS_CHARACTERSET" to the corresponding character set; 3. Just set the local PHP files to utf-8.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
What should I do if the php connection to oracle is garbled?
php connects to oracle to set the character set to avoid garbled characters
The database uses oracle. When php connects to oracle, it is best to specify the character set.
Check the PHP manual. The fourth parameter of oci_connect is charset, which is the key.
First get the character set of oracle and run "select * from V$NLS_PARAMETERS;". The variable NLS_CHARACTERSET corresponds to the character set we need. For example, here is "ZHS16GBK".
So, the final PHP code is:
$c1 = oci_connect("scott", "tiger", $db, 'zhs16gbk');
My local PHP files use utf-8, so the obtained code needs to be converted as follows:
while ($dat = oci_fetch_row($cur)) { print_r(iconv('gb2312', 'utf-8', $dat[0])); }
Update:
Colleagues said that Oracle can provide data according to the character set specified by the client. In other words, if my local PHP file uses UTF-8, then when I use oci_connect, I can directly specify utf-8, and even the encoding conversion will be saved.
$c1 = oci_connect("scott", "tiger", $db, 'UTF8');
Php code
while ($dat = oci_fetch_row($cur)) { print_r($dat[0]); }
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What should I do if the php connection to Oracle is garbled?. For more information, please follow other related articles on the PHP Chinese website!