[英]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 );
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 inUTF-8
, you do not need this function.如果您的文本已经是
UTF-8
,则不需要此功能。 In fact, applying this function to text that is not encoded inISO-8859-1
will most likely simply garble that text.实际上,将此函数应用于未在
ISO-8859-1
编码的文本很可能只会简化该文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.