How to return json_encode?

Asked

Viewed 163 times

0

I need to return a select made in PHP. It is working normally, but when displaying the result it returns me an error at the end. I need to return one json_encode to the android recognize. Follow the programming below

mysql_connect('maquina','aa','senha');
mysql_select_db('aaaa') or die (mysql_error());



$Cod_Empresa = $_GET['aaaa'];
$UC = $_GET['sss'];
$di = $_GET['dddd'];
$df = $_GET['ssdddd'];
$tensao  = strtoupper($_GET['eeeeeeeeeeeee']);

if (($di != "") && ($df != ""))
    {
        $periodo = '
        && D.Mes_Ref >= "'.$di.'"
        && D.Mes_Ref <= "'.$df.'"
            Order By Mes_Ref DESC
                ';
    }
else
    {
        $periodo = '
            Order By Mes_Ref DESC
            Limit 0,12
                ';
    }
switch($tensao)
    {
        case 'BT':
            $tensao_ = "";
            break;
        case 'AT':
            $tensao_ = "
                && D.Tip_Fatur = 0
                && D.Classe in ('A1','A2','A3','A3a')
            ";
            break;
        case 'MT':
            $tensao_ = "
                && D.Tip_Fatur = 0
                && D.Classe in ('A4','As')
            ";
            break;
        case 'ML':
            $tensao_ = "
                && D.Tip_Fatur = 1
            ";
            break;
    }



switch($tensao)
{
    case 'BT':
        $sql = "SELECT Mes_Ref, round(Total_Fatura,2) as Valor_em_Reais                                                 
                FROM Tab_Fatura_BT D
                    WHERE Cod_Empresa = ".$Cod_Empresa."
                    && Cod_UC = ".$UC."                     
                            ".$periodo."    
                                            ";
                                        break;
    default:
        $sql = "SELECT  D.Mes_Ref,round(V.Valor_Total_cor,2) as Valor_em_Reais                                  
                FROM Tab_Fatura_Dados D, Tab_Fatura_Valores V, Tab_Fatura_Leituras L
                    WHERE D.Cod_Empresa = ".$Cod_Empresa."
                    && D.Cod_UC = ".$UC."
                    && V.Cod_Empresa = D.Cod_Empresa
                    && V.Cod_UC = D.Cod_UC
                    && V.Cod_Fatura = D.Cod_Fatura
                    && L.Cod_Empresa = D.Cod_Empresa
                    && L.Cod_UC = D.Cod_UC
                    && L.Cod_Fatura = D.Cod_Fatura
                        ".$tensao_."
                            ".$periodo." 
                                            ";
                                        break;
}

            $query = mysql_query( $sql ) or die('Could not query');     

        for($rows = array(); $row = mysql_fetch_array($query); $rows[] = $row);
                {   

                    linha 99 $rows[] = $row->Mes_Ref;
                    linha 100 $rows[] = number_format($row->Valor_em_Reais,2,'.',',');
                    echo json_encode($rows);
                } 

?>

This returns me the following error:

Imagem tirada do erro

  • Hello, Try doing "echo json_encode($Rows);" after for

  • Returned me the same mistake!

  • Which is line 99 and 100 ?

  • Are these two $Rows[] = $Row->Mes_ref; $Rows[] = number_format($Row->Valued,'.',',');

  • if I just display json_encode without lines 99 and 100 it returns me correctly.. but however I need to treat these lines to resume a more presentable value... but with these two lines ai generates me an error presented above..

  • change $Row->Mes_ref to $Row[Mes_ref] and $Row[Valued].

Show 1 more comment

1 answer

0

The error may be in your php, check there in the code if the name, password and address of the bd. gives a look if ta td right...in this site www.hostinger.com vc can up your php and make your tests in real domain and free. with me gave the same error and the problem was the name of a table in php code...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.