简体   繁体   中英

sql query is returning a value, but $result on php is always an empty array

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() . "\n";
}

?> 

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_kan ji 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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