Access direct key

0

I would like to access the direct key of an array object without having to loop (foreach)

Instead of being like this:

    $nome = "";
    $codigo   = 0;
    $objeto  = DB::select( "SELECT NOME FROM TABELA WHERE CODIGO = ?", array( $codigo ) );
    foreach ( $objeto as $obj ){
        $nome = $obj->nome;
    }
    echo $nome;

I wish I did not need foreach

Type

   $nome = $objeto->nome;

But it gives the following message:

  

Trying to get property of non-object

    
asked by anonymous 26.10.2017 / 21:43

2 answers

2

Your query is wrong:

SELECT NOME WHERE ID = ?

Missing FROM [NOME DA TABELA AQUI] , should look like this:

DB::select( "SELECT NOME FROM minhatabela WHERE ID = ?", $nome );

26.10.2017 / 21:55
0

When you make a query it returns you an array, if you do not pass through a foreach it will not bring the attributes. That in the attributes case is an index of this array!

    
26.10.2017 / 22:03