PHP OCI8 extension does not support binding nchar/nvarchar2 variables as it always uses SQLCS_IMPLICIT for the character set form. With the help of UNISTR function, we can encode Unicode characters as ‘\xxxx’ where ‘xxxx’ is the hexadecimal value of a character in UCS-2 encoding format. UNISTR function returns a string in the national character set which all of our nchar, nvarchar2 or nclob columns have.
At PHP, initially we need to convert our string to the form that UNISTR function would understand. For this purporse we use iconv routines to convert our string to UCS-2BE character set and then translate each character to ‘\xxxx’ form. Then we surround each ncharor nvarchar2 bind variable (which might be either a table column or PL/SQL function or procedure parameter) with UNISTR function.
Below is an example which inserts a row into a table with nvarchar2 data.
<? function utf82unistr($s, $charset='UTF-8') { $cs = 'UCS-2BE'; $s = iconv($charset, $cs, $s); $us = ''; for ($i = 0; $i < iconv_strlen($s, $cs); $i++) { $c = iconv_substr($s, $i, 1, $cs); $us .= '\\' . bin2hex($c); } return $us; } $conn = OCILogon("user", "pass", "db"); if ($conn == false){ die(print_r(OCIError($conn), true)); } // utf-8 encoded text: Здравствуй, Мир $text = "\xd0\x97\xd0\xb4\xd1\x80\xd0\xb0\xd0\xb2\xd1\x81\xd1\x82\xd0\xb2\xd1\x83\xd0\xb9\x2c\x20\xd0\x9c\xd0\xb8\xd1\x80"; $text = utf82unistr($text); $stmt = OCIParse($conn, "INSERT INTO tbl(ncol) VALUES(UNISTR(:utxt))"); OCIBindByName($stmt,":utxt", $text, 4000); if (!OCIExecute($stmt)) { die(print_r(OCIError($stmt), true)); } OCIFreeStatement($stmt); ?>
Naujausi komentarai