Different SQL result with same query in PHP and MySQL

喜你入骨 提交于 2019-12-11 19:22:56

问题


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).

I use a PEAR framework for the connection with MySQL(5.0.26)

I have the SQL request :

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.

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 !

$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)

Thank you for your help


回答1:


Note that your connection to the database also has a CHARSET and a COLLATION for any string values you include in your query. Although your query looks the same to you in both situations, it must not from the MySQL servers point of view.

Maybe the client CHARSET (and/or COLLATION) differ when you connect via PHP from when you connect via MySQL console.

See the MySQL manual for more information on the client charset and collation.

For comparison, you can use this query:

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


来源:https://stackoverflow.com/questions/17315741/different-sql-result-with-same-query-in-php-and-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!