dongmibeng5885 2014-11-18 01:43
浏览 79

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?

  • 写回答

1条回答 默认 最新

  • douguanya6399 2014-11-18 06:00
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?