sql查询返回一个值,但php上的$ result总是一个空数组

I have this .php file(there's a lot of echo here, but that was just me debbugging):

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<?php
try 
{
$host="localhost:2222";
$username="karutakanji"; //replace with database username 
$password="123456"; //replace with database password 
$db_name="pairg_karutakanji_app"; //replace with database name

$con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_set_charset("utf8");
mysql_select_db("$db_name")or die("cannot select DB");

$nome_usuario = isset($_POST['nome_usuario']) ? $_POST['nome_usuario'] : '';
$categorias = isset($_POST['categorias']) ? $_POST['categorias'] : '';
$sql = "select count(kanji) as quantas_vezes_errou_o_kanji,kanji,nome_categoria from (select kanji,nome_categoria from (select kanji,id_categoria from (select id from (Select id_partidas_treinamento from partidas_treinamento inner join usuario on partidas_treinamento.id_usuario = usuario.id_usuario where nome_usuario='$nome_usuario') as tabelacomidspartidasdousuarioinformado inner join partidas_treinamento_kanjis on partidas_treinamento_kanjis.id_partidas_treinamento = tabelacomidspartidasdousuarioinformado.id_partidas_treinamento where se_acertou='não') as tabelaidskanjisusuarioerrou inner join jlptnew on jlptnew.id = tabelaidskanjisusuarioerrou.id) as kanjieidcategoriakanjiserrados inner join categorias where categorias.id_categoria = kanjieidcategoriakanjiserrados.id_categoria) as kanjisenomecategoriakanjiserrados "; 

$array_categorias = explode("," , "$categorias");
$i = 0;
$len = count($array_categorias);

$array_categorias = array_filter($array_categorias);
if (!empty($array_categorias)) 
{
    $sql = $sql . "where (";    
    while($i < $len)
    {
        $uma_categoria = $array_categorias[$i];
        $sql = $sql . "nome_categoria = '" . $uma_categoria . "'";
        if ($i !== $len - 1) 
        {
                $sql = $sql . " or "; 
        }
        $i = $i + 1;
    }
    $sql = $sql . ")";
}

$sql = $sql . " GROUP BY kanji ORDER BY quantas_vezes_errou_o_kanji DESC"; 
mysql_set_charset("utf8");
$result = mysql_query($sql);
if(!$result)
{
    echo mysql_error($con);
}


echo "/////////////////" . $sql . "///////////////////////////";
$json = array();

if(mysql_num_rows($result)){
    while($row=mysql_fetch_assoc($result)){
        $json[]=$row;
    }
} 


mysql_close($con);
array_walk_recursive($json, function (&$item, $key) { if (is_string($item)) $item = mb_encode_numericentity($item, array (0x80, 0xffff, 0, 0xffff), 'UTF-8'); });
    echo mb_decode_numericentity(json_encode($json), array (0x80, 0xffff, 0, 0xffff), 'UTF-8');

}
catch (Exception $e) {
 echo $e;
 echo 'Caught exception: ' .  $e->getMessage() . "
";
}

?> 

I am using some debbuging on my eclipse based on the echo i put on the php. I do this:

httpPost.setEntity(new UrlEncodedFormEntity(param));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            String mensagemEntity1 = EntityUtils.toString(httpEntity);
            mensagemEntity1 = mensagemEntity1 + "";

And the debbuger tool from eclipse adt(i am using the .php file on an Android project) says my sql is like this:

select count(kanji) as quantas_vezes_errou_o_kanji,kanji,nome_categoria from (select kanji,nome_categoria from (select kanji,id_categoria from (select id from (Select id_partidas_treinamento from partidas_treinamento inner join usuario on partidas_treinamento.id_usuario = usuario.id_usuario where nome_usuario='fabioandrews') as tabelacomidspartidasdousuarioinformado inner join partidas_treinamento_kanjis on partidas_treinamento_kanjis.id_partidas_treinamento = tabelacomidspartidasdousuarioinformado.id_partidas_treinamento where se_acertou='não') as tabelaidskanjisusuarioerrou inner join jlptnew on jlptnew.id = tabelaidskanjisusuarioerrou.id) as kanjieidcategoriakanjiserrados inner join categorias where categorias.id_categoria = kanjieidcategoriakanjiserrados.id_categoria) as kanjisenomecategoriakanjiserrados where (nome_categoria = 'Saúde' or nome_categoria = 'Educação' or nome_categoria = 'Supermercado' or nome_categoria = 'Tempo') GROUP BY kanji ORDER BY quantas_vezes_errou_o_kanji DESC

When i execute this sql query on the mysql workbench, it works fine and i receive my results. However, on the .php, the $result from it is always an empty string like this: '[]'

No message errors are found, nothing. Why is my result an empty string if my sql query is right?

dougaopu7938
dougaopu7938 我试图将utf-8更改为葡萄牙语编码,仍然是[]空数组。我试图使用没有ç或á的nome_categoria,但仍然没有变化。Sorri我没有声誉回答你自己的帖子上的评论
接近 6 年之前 回复

1个回答



我会查看php文件编码。</ p>

行mysql_set_charset(“utf8” “); </ code>将编码设置为UTF-8。 </ p>

真的是用UTF-8编写的查询吗? 如果不是where子句:</ p>

  nome_categoria ='Saúde'或nome_categoria ='Educação'或nome_categoria ='Supermercado'或nome_categoria ='Tempo'
</ code> < / pre>

将过滤所有结果,并且不会返回任何行。</ p>
</ div>

展开原文

原文

I would check the php file encoding.

The line mysql_set_charset("utf8"); sets the encoding to UTF-8.

Is really the query written in UTF-8? If not the where clauses:

nome_categoria = 'Saúde' or nome_categoria = 'Educação' or nome_categoria = 'Supermercado' or nome_categoria = 'Tempo'

will filter all results and will return no rows.

du9826
du9826 用于保存php文件的编码是什么? 您可以使用notepad ++进行检查
接近 6 年之前 回复
dongshi1148
dongshi1148 我试图将utf-8更改为葡萄牙语编码,仍然是[]空数组。 我试图使用没有ç或á的nome_categoria,但仍然没有变化
接近 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐