Help with json and adodb in PHP

Asked

Viewed 212 times

0

good afternoon,

the following code returns me in json... but is in Pdo.. as would be in adodb?

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

Edit:

I tried this, but it doesn’t 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 select data by inserting this data into the array. and turn the array into json..

returns the empty json.. but the query, by the test I did, returns data

json result:

{"retorno": }

print_r result($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 this.. i.e. it is not accepting utf-08.. when I remove the special characters json works...

Somebody knows how to fix this?

  • creates a loop and assigns the array after it echo json_encode($dados); if I’m not mistaken ADOBD uses a méotodos next() and the props EOF or BOF

  • Does an error appear? more specifically what would 'not work'?

  • Could put the result of: print_r($retorno) in the question.

2 answers

0

I suggest you go through two independent trials. So you’ll know where the mistake is.

  1. Create your array, regardless of how you connect to the database.
  2. Turn your array into JSON.

After making 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 turn your array into JSON:

$myJSON = json_encode(array($arrTot));
  • right... edited and now shows this error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes) in C: xampp htdocs api v1 index.php on line 20... edited my question

  • the information you’re looking for in the database is too big? Usually I drain the memory of php with a query of 100,000 lines (depending on the content). If this is the problem increase the mention of php. The limit is 2gb. If you overcome this, you will have to: 1-Change strategy or 2-Switch to PHP 7 that has unlimited memory, up to the maximum available on the system.

  • Check out this post http://stackoverflow.com/questions/33874579/what-is-the-limit-in-size-of-a-php-variable-when-storing-string

0

You can turn 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 if the charset is set utf8

  • right... where Seto the charset for utf8?

  • is mysql database? you can do so: $db->SetCharSet('utf8');

  • 1

    the bank is oracle 11g

Browser other questions tagged

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