return tinyint as boleano in json Ncode

Asked

Viewed 45 times

0

I make a fetchall with Pdo in the database and receive the result correctly, but the values are saved in tinyint in the database, and I get them in 1 and 0, so when I do json Ncode, it returns me 0 and 1, wanted to return true or false. I tried to make a foreach to convert but so it returns only the line I converted.

$usermail = $request->getHeader('PHP_AUTH_USER');
$senha = $request->getHeader('PHP_AUTH_PW');

$sql = new Sql();
$user =  new Usuario();
$autenticado = $user->login($usermail[0],$senha[0]);

if ($autenticado) {
    $resultado = $sql->select("SELECT * FROM tb_alunos WHERE email = :EMAIL LIMIT 1",array(
        ":EMAIL"=>$usermail[0]
    ));
    foreach( $resultado as $row ) {
        $array[] = array('liberadoexercicio' => (bool)$row['liberadoexercicio']);
    }
    $response = json_encode(array_shift($array),JSON_NUMERIC_CHECK);
    return $response;
}else{
    return $response->withStatus(401);
}

2 answers

2


The error is that basically what you are doing is capturing all the table information tb_alunos > Saving only the data liberadoexercicio in the variable $array > Deleting the first record with array_shift and displaying the json on the screen.

You can do the following to return the data as bool

<?php

$resultado = $sql->select("SELECT * FROM tb_alunos WHERE email = :EMAIL LIMIT 1",array(
    ":EMAIL"=>$usermail[0]
));

/* Se você quiser retornar apenas o campo `liberadoexercicio` utilize a query abaixo (é melhor quanto a performance) */
/* SELECT liberadoexercicio FROM tb_alunos WHERE email = :EMAIL LIMIT 1 */

foreach( $resultado as &$row ) {
    $row["liberadoexercicio"] = (bool)$row["liberadoexercicio"];

    //$row["liberadoexercicio"] = $row["liberadoexercicio"] == 1; // Outra forma
}

echo json_encode($resultado);

The & means that $row is a reference to a variable, as I do not know its level of orientation to objects, I will leave the link to read.

  • from where it’s coming out $students?

  • Got it, it’s the result of the consultation, it worked, thank you

0

You can check when you arrow the array using a conditional (if, switch, ternary)

$usermail = $request->getHeader('PHP_AUTH_USER');
$senha = $request->getHeader('PHP_AUTH_PW');

$sql = new Sql();
$user =  new Usuario();
$autenticado = $user->login($usermail[0],$senha[0]);

if ($autenticado) {
    $resultado = $sql->select("SELECT * FROM tb_alunos WHERE email = :EMAIL LIMIT 1",array(
        ":EMAIL"=>$usermail[0]
    ));
    foreach( $resultado as $row ) {
        // modo 1
        if($row['liberadoexercicio'] == 1)
            $res = true;
        else
            $res = false;
        $array[] = array('liberadoexercicio' => $res);  
        // modo 2
        $array[] = array(
            'liberadoexercicio' => ($row['liberadoexercicio'] == 1) ? true : false
        );
    }
    $response = json_encode(array_shift($array),JSON_NUMERIC_CHECK);
    return $response;
}else{
    return $response->withStatus(401);
}
  • it returns only this line "released exercise", I want to display the whole result by changing this 1 to true for example

Browser other questions tagged

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