简体   繁体   English

在PHP和MySQL中具有相同查询的不同SQL结果

[英]Different SQL result with same query in PHP and MySQL

After a quick search, I haven't find solution of my problem so I post a new one. 快速搜索后,我没有找到解决问题的方法,因此我发布了一个新的解决方法。

So I have to create view in a MySQL database from a Web interface (using PHP). 因此,我必须从Web界面(使用PHP)在MySQL数据库中创建视图。

I use a PEAR framework for the connection with MySQL(5.0.26) 我将PEAR框架用于与MySQL的连接(5.0.26)

I have the SQL request : 我有SQL请求:

CREATE VIEW test AS SELECT cswc.code_de_la_copie, cswc.service_de_reference, cswc.libelle_de_la_copie, cswc.direction_de_securite_logique
FROM  pressi_copiesServiceWithCibles cswc LEFT OUTER JOIN pressi_servicesReferenceWithCibles srwc ON cswc.service_de_reference = srwc.code_du_service
WHERE cswc.cible is null
  AND (srwc.cible LIKE '%£DOMAIN£%' OR srwc.cible LIKE '%$DOMAIN$%');

When I execute this request directly on mon local MySQL Database, I obtain a result with 470 lines. 当我直接在本地MySQL数据库上执行此请求时,获得了470行的结果。

However, when I execute this request in my PHP code, I have a different result (I have 386 line), and I don't know why ! 但是,当我在PHP代码中执行此请求时,结果却不同(我有386行),我也不知道为什么!

$values['request'] = "SELECT cswc.code_de_la_copie, cswc.service_de_reference, cswc.libelle_de_la_copie, cswc.direction_de_securite_logique
FROM  pressi_copiesServiceWithCibles cswc LEFT OUTER JOIN pressi_servicesReferenceWithCibles srwc ON cswc.service_de_reference = srwc.code_du_service
WHERE cswc.cible is null
  AND (srwc.cible LIKE '%£DOMAIN£%' OR srwc.cible LIKE '%$DOMAIN$%');";

$baseView = "test";

$sqlView = 'CREATE VIEW '.$baseView.' AS '.$values['request'];
$res =& $this->mdb2->query($sqlView);
if (PEAR::isError($res)) {
   return false;
}

Moreover, I have already create 6 views before this one without any problem (same result in PHP and in MySQL) 此外,在此之前我已经创建了6个视图,没有任何问题(PHP和MySQL中的结果相同)

Thank you for your help 谢谢您的帮助

Note that your connection to the database also has a CHARSET and a COLLATION for any string values you include in your query. 请注意,与数据库的连接还对您包含在查询中的任何字符串值都有一个CHARSETCOLLATION Although your query looks the same to you in both situations, it must not from the MySQL servers point of view. 尽管您的查询在两种情况下看起来都一样,但从MySQL服务器的角度来看,绝对不能。

Maybe the client CHARSET (and/or COLLATION ) differ when you connect via PHP from when you connect via MySQL console. 通过PHP连接时,与通过MySQL控制台连接时,客户端CHARSET (和/或COLLATION )可能有所不同。

See the MySQL manual for more information on the client charset and collation . 有关客户端字符集和排序规则的更多信息,请参见MySQL手册

For comparison, you can use this query: 为了进行比较,您可以使用以下查询:

SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM