0

I'm really having trouble trying to execute the function below:

function retrieve_as_JSON($sql_statement)
{
    $r = oquery($sqli); // oquery runs all that conn()->query() stuff
    if($r->num_rows)
    {
        $arr = array();
        while($tmp = $r->fetch_array( MYSQLI_ASSOC ))
        {
            $arr[] = json_encode($tmp);
        }
        return json_encode($arr);
    }
}

$tmp in the loop is:

Array ( [code] => ecb36c8e9e70b1622fb85ce1af7ba824 
        [prsn] => a6abd41ca4376f1ccb5d8425e9e97ca6 
        [type] => Isento 
        [motn] => Nome da Mãe 
        [natu] => Joseense 
        [nati] => Brasileiro 
        [mrts] => Solteiro(a) 
        [conj] => Nome da esposa 
        [prof] => Analista/Programador 
        [skcl] => Caucasiano/Branca 
        [lafr] => 0 )

The result is "[false]". Without the json_encode(), it return a regular array() filled with information. Using just one time the json encode function, on return or inside the while, it still gives nothing...

Any help?

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
rafsb
  • 31
  • 4
  • 3
    You're double-json-encoding. Remove the `json_encode($tmp)` and instead just make that `$arr[] = $tmp;` – random_user_name Jan 23 '17 at 21:38
  • What are the values of `$tmp`? Does your loop run? Are you sure? Your question is fairly unclear / confusing. – random_user_name Jan 23 '17 at 21:39
  • What is `$r` that you're attempting to use??? – AbraCadaver Jan 23 '17 at 21:40
  • `error_reporting(E_ALL); ini_set('display_errors', '1');` – AbraCadaver Jan 23 '17 at 21:41
  • @cale_b, i did this before (removing json_encode($tmp))... no success... still got ""... if I remove all json_encode() from this code it return the result as a normal php array() with data, so the loop is running... – rafsb Jan 23 '17 at 21:48
  • 1
    OK, so what's the value of $tmp? It's impossible to help if you don't want to answer the questions that we ask in comments. – random_user_name Jan 23 '17 at 21:49
  • sorry... $tmp = Array ( [code] => ecb36c8e9e70b1622fb85ce1af7ba824 [prsn] => a6abd41ca4376f1ccb5d8425e9e97ca6 [type] => Isento [motn] => Nome da Mãe [natu] => Joseense [nati] => Brasileiro [mrts] => Solteiro(a) [conj] => Nome da esposa [prof] => Analista/Programador [skcl] => Caucasiano/Branca [lafr] => 0 ) – rafsb Jan 23 '17 at 21:51
  • @Don'tPanic, thanks but i doesn't work, the return result is ""... – rafsb Jan 23 '17 at 21:56
  • @AbraCadaver question edited... $result wasn't in my code, it was $r... – rafsb Jan 23 '17 at 22:03
  • Have you tried checking [`json_last_error`](http://php.net/manual/en/function.json-last-error.php#refsect1-function.json-last-error-examples) to see why the `json_encode` fails? – Don't Panic Jan 23 '17 at 22:07
  • @Don'tPanic, "Malformed UTF-8 characters, possibly incorrectly encoded"... do you know the meaning? utf8_encode didn't work.... – rafsb Jan 23 '17 at 22:13
  • [Check this out and see if it helps.](http://stackoverflow.com/a/9099949/2734189) – Don't Panic Jan 23 '17 at 22:21
  • @Don'tPanic great!!! worked.... thanks... – rafsb Jan 23 '17 at 22:30

0 Answers0