Help with json and adodb in PHP

0

Good afternoon,

The following code returns me in json ... but it's in pdo .. what would it be like in adodb?

$users = $stmt->fetchAll(PDO::FETCH_OBJ);
echo '{"users": ' . json_encode($users) . '}';

edit:

I tried this, but it does not work:

if($db){
            $stmt = $db->Execute($sql);

            $retorno = array();
            while(!$stmt->EOF){
                $retorno[] = $stmt->fields;
                $stmt->MoveNext();
            }

            echo '{"retorno": ' . json_encode($retorno) . '}';    
        }

What I tried to do in the above code was to go through the recordset with the data of the select, inserting this data into the array. and transform the array into json ..

I get the empty json .. but the query, by the testo I made, returns me data

json's result:

{"retorno": }

result of print_r ($ return);

Array ( [0] => Array ( [0] => [VENDA] => [1] => 1 [PRODUTO] => 1  ) )

edit:

if($db){
            $stmt = $db->Execute($sql);

            $retorno = array();
            while($res = $stmt->GetArray()){
                $retorno[] = $res;
            }

            echo '{"retorno": ' . json_encode($retorno) . '}';    
        }

solution!

if($db){
            $stmt = $db->Execute($sql);
            $retorno = $stmt->GetArray();

            //var_dump($retorno);
            echo '{"retorno": ' . json_encode($retorno) . '}';    

            if( $jret = json_encode($retorno) ){ echo "nice"; } else{ echo "Fail"; }

        }

however ... my data has a special character like "ç" and json is not accepting that .. ie .. is not accepting utf-08 .. when removing the special characters json works ...

Does anyone know how to solve this?

    
asked by anonymous 12.05.2016 / 19:36

2 answers

0

I suggest you go through two independent processes. So you'll know where the error is.

  • Create your array, regardless of how you connect to the database.
  • Transform your array into JSON.
  • After doing your loop, you will create a multi level array $arrTot

    $res = $db->Execute($sql);//Nota: Precisa adequar o código ao seu cenário
    
    if ($res){
    
             while ($arr = $res->GetArray()) {
    
                  $arrTot[]= $arr;  
    
             }
    }
    

    Then transform your array into JSON:

    $myJSON = json_encode(array($arrTot));
    
        
    12.05.2016 / 21:06
    0

    You can transform into Array and then use json_encode.

    $sql = 'SELECT * BLA BLA BLA...';
    
    if (!($result = $db->Execute($sql))):
       throw new Exception($db->ErrorMsg());
    endif;
    
    var_dump($result->_array);  // veja primeiro se o seu array esta ok
    
    //echo json_encode($result->_array); // mostre o json
    

    Also check that charset is set to utf8

        
    12.05.2016 / 22:44