简体   繁体   English

相同的选择适用于控制台但不适用于PHP

[英]Same select works on console but not in PHP

this is getting me mad. 这让我很生气。 This is the code I'm using right now: 这是我现在使用的代码:

   <?php
//open connection to mysql db
$connection = mysqli_connect( "mysql.kit-series.net", "XXXXX", "XXXXXX", "kit_series_net" ) or die( "Error " . mysqli_error( $connection ) );
$connection->set_charset( "utf8" );

//fetch table rows from mysql db
$nombre = $_POST['nombre'];
$select = "SELECT ID FROM wp_qnnpx4_posts WHERE post_type='tvshows' AND post_title = '" . $nombre . "'";
$result = $connection->query( utf8_encode( $select ) );
$result = $result->fetch_assoc();
$id     = $result['ID'];
echo $select;
echo $id;

//close the db connection
mysqli_close( $connection );
  • The result it's the expected if I use the select on console. 如果我在控制台上使用select,结果就是预期的结果。
  • The result it's the expected if I use the select in the PHP and the $nombre varible has no accents. 如果我在PHP中使用select并且$ nombre varible没有重音,那么结果是预期的。
  • The result it's the expected if I use the select in the console and the $nombre varible has accents. 如果我在控制台中使用select并且$ nombre varible具有重音符号,则结果是预期的。

Only when $nombre has accents and is used on the PHP file, the result isn't the expect, and it just return no results. 只有当$ nombre具有重音并且在PHP文件上使用时,结果才是期望的,并且它只返回没有结果。

I printed the select in that case, put it on console, and it works. 我在那种情况下打印了选择,把它放在控制台上,它可以工作。 I dind't know what more to do. 我不知道还能做什么。

Any idea? 任何想法?

Don't use utf8_encode unless you are processing non-UTF8 strings. 除非您正在处理非UTF8字符串,否则请勿使用utf8_encode From your question and comments it turns out that you are completely on UTF8 (HTML, database, database connection, PHP file encoding), so calling utf8_encode is not only not necessary, it will have averse effects. 从您的问题和评论中可以看出,您完全使用UTF8(HTML,数据库,数据库连接,PHP文件编码),因此调用utf8_encode不仅不是必需的,而且会产生反作用。

As said in an appreciated contribution to the PHP docs on utf8_encode : 正如utf8_encode上PHP文档的赞赏所说:

If your text is not encoded in ISO-8859-1 , you do not need this function. 如果您的文本未在ISO-8859-1编码,则不需要此功能。 If your text is already in UTF-8 , you do not need this function. 如果您的文本已经是UTF-8 ,则不需要此功能。 In fact, applying this function to text that is not encoded in ISO-8859-1 will most likely simply garble that text. 实际上,将此函数应用于未在ISO-8859-1编码的文本很可能只会简化该文本。

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

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