Difficulty with PDO

Asked

Viewed 34 times

2

I’m starting with the use of PDO and I have a doubt that I could not solve. I do the select below that will return me 2 hours:

$sqlSaida = 'SELECT horario 
               FROM HORARIOS
              WHERE COD = :codsaida 
              UNION
             SELECT horario 
               FROM HORARIOS
              WHERE COD = :codretorno;';

$resSaida = $conexao->prepare($sqlSaida);

$resSaida->execute(array(
    ':codsaida' => $codsaida,
    ':codretorno' => $codretorno
));

$saidas = $resSaida->fetchAll();

But I can’t store the results, below I can display both, but how could I store them in different variables?

foreach ($saidas as $saida) {
 echo $saida['horario'];
}
  • What is the purpose of these variables?

  • I need to pass them on another select forward.

  • Which select from the front? did not give to understand what is your goal with that code.

  • i do this select and I will use the result in another select and then in an Insert.

  • Edit the question and add this information and code as well. This query returns a line or more?

  • The answer below solved my doubt. Thank you.

  • The response mounted array has the same structure as $saidas. If it worked out for you :)

Show 2 more comments

1 answer

1


Creates a array.

    $arrData = []; 
    $x = 0;
    foreach ($saidas as $saida) {
       $arrData[$x]['horario'] = $saida['horario'];
       $arrData[$x]['retorno'] = $saida['retorno'];
       $x++;
    }

    echo "<pre>";
    print_r($arrData['horario']);
    echo "</pre>";
  • This ai code will return an array with the last element of $saidas.

  • It’s true. I answered in a hurry. Thanks.

  • 1

    Exactly that! Thank you very much!

Browser other questions tagged

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