将重音字符从 PHP 脚本保存到 Oracle DB

发布于 2024-09-15 08:54:18 字数 641 浏览 3 评论 0原文

我正在尝试将重音字符 èòàèì 保存到 Oracle DB 的 VARCHAR2 字段中。我已经放了

<html>
<head>
   <meta http-equiv="Content-type" value="text/html; charset=utf-8">
</head>
<body>
<?php
 header('Content-type: text/html; charset=utf-8');

....
//在这里我将插入到数据库中:

$str=utf8_encode("JeanPièrre"); // or $str="JeanPièrre" ... is the same, it does not run
$sql="insert into TABLE(nvar) values('".$str."')";
$stmt = oci_parse($ora_conn, $sql) or die(oci_error().$query);
oci_execute($stmt);

但是重音字符没有正确保存,我看到JeanPi??rre

我能做什么?请帮助我。
提前致谢!

I'm trying to save accented chars èòàèì to an Oracle DB in a VARCHAR2 field. I've put

<html>
<head>
   <meta http-equiv="Content-type" value="text/html; charset=utf-8">
</head>
<body>
<?php
 header('Content-type: text/html; charset=utf-8');

....
//and here i make the insert into the DB:

$str=utf8_encode("JeanPièrre"); // or $str="JeanPièrre" ... is the same, it does not run
$sql="insert into TABLE(nvar) values('".$str."')";
$stmt = oci_parse($ora_conn, $sql) or die(oci_error().$query);
oci_execute($stmt);

But accented character is not saved correctly, I see JeanPi??rre

What can I do? Pls help me.
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

客…行舟 2024-09-22 08:54:29

我已经更改了 php.ini 文件中的设置并且它起作用了。
需要在 php.ini 中启用以下内容

mbstring.internal_encoding = UTF-8;

I have changed the setting in php.ini file and it worked.
The following needs to be enabled in php.ini

mbstring.internal_encoding = UTF-8;
时光沙漏 2024-09-22 08:54:28

我很关心这条线。

$sql="insert into TABLE(nvar) values('".$str."')";

它不是通过变量绑定字符串值,而是简单地将其连接到插入中。这在 Oracle 中是不好的做法。绑定用法的一个例子是:

$sql = "insert into TABLE(nvar) values( :str )";
$stmt = oci_parse($ora_conn, $sql) or die(oci_error().$query);
ocibindbyname($stmt,":str",&$str,6);
oci_execute($stmt);

所以我首先解决这个问题。解决该错误的可能性很小。 这里提到了,但它不是特别清楚。 doing 之间在文字上存在一些差异

INSERT INTO table VALUES ('è');

基本上它意味着doing和

INSERT INTO table VALUES (N'è');

I'm concerned about this line.

$sql="insert into TABLE(nvar) values('".$str."')";

Rather than binding the string value through a variable, it is simply concatenating it into the insert. This is poor practice in Oracle. An example of a bind usage is :

$sql = "insert into TABLE(nvar) values( :str )";
$stmt = oci_parse($ora_conn, $sql) or die(oci_error().$query);
ocibindbyname($stmt,":str",&$str,6);
oci_execute($stmt);

So I'd start by fixing that. There's a small chance that will resolve the error. It gets mentioned here but it isn't particularly clear. Basically it implies there's some difference regarding literals between doing

INSERT INTO table VALUES ('è');

and

INSERT INTO table VALUES (N'è');
葬﹪忆之殇 2024-09-22 08:54:28

我遇到了同样的问题并使用 utf8_decode(); 修复了它

    $textForDB = utf8_decode("Áà");

I had the same problem and fixed it with utf8_decode();

    $textForDB = utf8_decode("Áà");
古镇旧梦 2024-09-22 08:54:25

如果您的源文件包含以下行:

$str=utf8_encode("JeanPièrre");

已经以 UTF-8 编码(我的意思是文件),那么您不必对“JeanPièrre”进行编码,因为它已经以 UTF-8 编码。

带编码的元标记仅用于显示或处理表单目的 - 它不会影响您的源代码和后端工作。

请记住,元标记和文件的编码类型不同。您可以通过许多程序检查和转换您的文件,请参阅notepad++,您应该致力于编码“UTF-8 without BOM”。

另外请确保您的数据库/表/连接设置为 UTF-8(如果有此类选项)。

还要检查您从数据库获取数据的方式 - 也许插入是可以的,但是在查询获取数据时字符被破坏。

If your source file, which has this line:

$str=utf8_encode("JeanPièrre");

is already encoded in UTF-8 (i mean just file), then you don't have to encode "JeanPièrre", because it's already in UTF-8.

Meta tag with encoding is only for display or handling forms purpose - it don't affect your source code and backend work.

Remember that meta tag and encoding type of file are not the same. You can check and convert yor files by many programs, see notepad++, you should work on encoding "UTF-8 without BOM".

Also be sure, that your DB/table/connection are set to UTF-8 if they have such option.

Check also your way to get data from database - maybe inserting is ok, but characters are broken when querying to fetch data.

想念有你 2024-09-22 08:54:24

你的 NLS_CHARACTERSET 是什么? (直接在数据库中设置)。
值可能与 NLS_LANG 不同(与客户端运行的编码不同)。
运行以下 SQL 查询时,您会发现该值:

SELECT PARAMETER, VALUE FROM NLS_DATABASE_PARAMETERS;

我的数据库的输出如下所示(我的 NLS_CHARACTERSET=AL32UTF8):

SQL> col PARAMETER format a25
SQL> col VALUE format a50
SQL> SELECT PARAMETER, VALUE FROM NLS_DATABASE_PARAMETERS;

PARAMETER                 VALUE
------------------------- --------------------------------------------------
NLS_LANGUAGE              AMERICAN
NLS_TERRITORY             AMERICA
NLS_CURRENCY              $
NLS_ISO_CURRENCY          AMERICA
NLS_NUMERIC_CHARACTERS    .,
NLS_CHARACTERSET          AL32UTF8
NLS_CALENDAR              GREGORIAN
NLS_DATE_FORMAT           DD-MON-RR
NLS_DATE_LANGUAGE         AMERICAN
NLS_SORT                  BINARY
NLS_TIME_FORMAT           HH.MI.SSXFF AM
NLS_TIMESTAMP_FORMAT      DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT        HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT   DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY         $
NLS_COMP                  BINARY
NLS_LENGTH_SEMANTICS      BYTE
NLS_NCHAR_CONV_EXCP       FALSE
NLS_NCHAR_CHARACTERSET    AL16UTF16
NLS_RDBMS_VERSION         9.2.0.6.0

20 rows selected.

问题可能出在您客户端的编码设置(我不是指 PHP 函数“utf8_encode”,而是真正编码了您的客户端)工作站,脚本运行的地方)。或者它是不同的编码数据库和值 NLS_CHARACTERSET。

此页面上描述了可能相同的问题:

http://www.databaseanswers.org/ora_nls_support.htm

What is your NLS_CHARACTERSET? (settings directly in the database).
Value may be different from NLS_LANG (different from encoding, wher client run).
This value you will find run the following SQL query:

SELECT PARAMETER, VALUE FROM NLS_DATABASE_PARAMETERS;

Output from my database look like this (my NLS_CHARACTERSET=AL32UTF8):

SQL> col PARAMETER format a25
SQL> col VALUE format a50
SQL> SELECT PARAMETER, VALUE FROM NLS_DATABASE_PARAMETERS;

PARAMETER                 VALUE
------------------------- --------------------------------------------------
NLS_LANGUAGE              AMERICAN
NLS_TERRITORY             AMERICA
NLS_CURRENCY              $
NLS_ISO_CURRENCY          AMERICA
NLS_NUMERIC_CHARACTERS    .,
NLS_CHARACTERSET          AL32UTF8
NLS_CALENDAR              GREGORIAN
NLS_DATE_FORMAT           DD-MON-RR
NLS_DATE_LANGUAGE         AMERICAN
NLS_SORT                  BINARY
NLS_TIME_FORMAT           HH.MI.SSXFF AM
NLS_TIMESTAMP_FORMAT      DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT        HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT   DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY         $
NLS_COMP                  BINARY
NLS_LENGTH_SEMANTICS      BYTE
NLS_NCHAR_CONV_EXCP       FALSE
NLS_NCHAR_CHARACTERSET    AL16UTF16
NLS_RDBMS_VERSION         9.2.0.6.0

20 rows selected.

The problem may be either with encoding settings your client (I do not mean PHP function "utf8_encode", but really encoding your workstation, where the script runs). Or it's different encoding database and the value NLS_CHARACTERSET.

Probably identical problem is described on this page:

http://www.databaseanswers.org/ora_nls_support.htm

柠檬色的秋千 2024-09-22 08:54:24

我通过将 NLS_LANG=American_America.WE8ISO8859P1 添加到 /etc/init.d/apache2 文件中的 ENV 变量解决了我的问题。

这使得 PHP 向 Oracle DB 发送正确的字符。

i've solved my problem by adding NLS_LANG=American_America.WE8ISO8859P1 to the ENV variable in /etc/init.d/apache2 file.

This made PHP sending correct chars to the Oracle DB.

相思故 2024-09-22 08:54:23

确保您的表具有 UTF8 字符集,并确保与数据库的连接使用 UTF8。搜索 Oracle 示例时我发现了这个:

resource oci_connect  ( string $username  , string $password  [, string $connection_string  [, string $character_set  [, int $session_mode  ]]] )

它将变成类似

$conn = oci_connect('insertUsername', 'insertPassword', 'insertHostname', 'AL32UTF8');

完整文档:

https://www.php.net /oci_connect

Make sure your tables have a UTF8 character set and make sure the connection with your database is using UTF8. Searching for a Oracle example I found this:

resource oci_connect  ( string $username  , string $password  [, string $connection_string  [, string $character_set  [, int $session_mode  ]]] )

Which will become something like

$conn = oci_connect('insertUsername', 'insertPassword', 'insertHostname', 'AL32UTF8');

Full documentation:

https://www.php.net/oci_connect

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文