MySQL Query仅适用于WordPress

时间:2022-09-18 13:14:37

I tried extensively to find an answer but my question seems to be too specific.

我广泛尝试找到答案,但我的问题似乎太具体了。

My query contains latin characters:

我的查询包含拉丁字符:

$mydb->query("SELECT cotação FROM compras");

It's working fine when called in a WordPress shortcode, but in a custom php on the same site it gives me this:

在WordPress短代码中调用它时工作正常,但在同一站点上的自定义php中,它给了我:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '??o FROM compras' at line 1

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第1行的'?? o FROM compras'附近使用正确的语法

Now, I don't want to just remove the characters because I know the query works and I'd rather understand the problem.

现在,我不想只删除字符,因为我知道查询有效,我宁愿理解这个问题。

So what's going on here? Thanks.

那么这里发生了什么?谢谢。

1 个解决方案

#1


To get this to work on my local machine, it would not work until I set the collation and NAMES to UTF8. It appears from the wealth of data I find in searching that this is the primary problem people encounter with non-ASCII characters.

为了让它在我的本地机器上工作,在我将校对和NAMES设置为UTF8之前它将无法工作。从我在搜索中发现的大量数据看来,这是人们遇到的非ASCII字符的主要问题。

To get it to work with my local PDO instance, I did the following:

为了使它能够与我的本地PDO实例一起使用,我执行了以下操作:

    $pdo->exec("SET collation_connection = utf8_bin");
    $pdo->exec("SET NAMES utf8");
    $all = $pdo->query("SELECT * FROM temp_cotação LIMIT 5")->fetchAll();

You can find more information here:

您可以在这里找到更多信息:

PDO cutting off strings at a UTF-8 character

PDO以UTF-8字符切断字符串

It seems to me that it would be better to do this globally rather than per connection...I found this to do the SET NAMES utf8 globaly for each connection:

在我看来,最好是全局而不是每个连接...我发现这为每个连接执行SET NAMES utf8全局:

How to make PDO run SET NAMES utf8 each time I connect, In ZendFramework

如何在每次连接时使PDO运行SET NAMES utf8,在ZendFramework中

To do the same thing in mysqli_, look here:

要在mysqli_中做同样的事情,请看这里:

php mysql SET NAMES 'utf8' COLLATE 'utf8_unicode_ci' doesn't work with mysqli (solved)

php mysql SET NAMES'utf8'COLLATE'utf8_unicode_ci'不适用于mysqli(已解决)

#1


To get this to work on my local machine, it would not work until I set the collation and NAMES to UTF8. It appears from the wealth of data I find in searching that this is the primary problem people encounter with non-ASCII characters.

为了让它在我的本地机器上工作,在我将校对和NAMES设置为UTF8之前它将无法工作。从我在搜索中发现的大量数据看来,这是人们遇到的非ASCII字符的主要问题。

To get it to work with my local PDO instance, I did the following:

为了使它能够与我的本地PDO实例一起使用,我执行了以下操作:

    $pdo->exec("SET collation_connection = utf8_bin");
    $pdo->exec("SET NAMES utf8");
    $all = $pdo->query("SELECT * FROM temp_cotação LIMIT 5")->fetchAll();

You can find more information here:

您可以在这里找到更多信息:

PDO cutting off strings at a UTF-8 character

PDO以UTF-8字符切断字符串

It seems to me that it would be better to do this globally rather than per connection...I found this to do the SET NAMES utf8 globaly for each connection:

在我看来,最好是全局而不是每个连接...我发现这为每个连接执行SET NAMES utf8全局:

How to make PDO run SET NAMES utf8 each time I connect, In ZendFramework

如何在每次连接时使PDO运行SET NAMES utf8,在ZendFramework中

To do the same thing in mysqli_, look here:

要在mysqli_中做同样的事情,请看这里:

php mysql SET NAMES 'utf8' COLLATE 'utf8_unicode_ci' doesn't work with mysqli (solved)

php mysql SET NAMES'utf8'COLLATE'utf8_unicode_ci'不适用于mysqli(已解决)