Home > front end >  Populate a variable in controller with single row data from a query
Populate a variable in controller with single row data from a query

Time:02-18

I have in a Codeigniter 4 controller this code:

    $competicion =$db->table('competiciones')
    ->select('*')
    ->where('competiciones.slug', $blog_slug)
    ->get()->getRowArray();

How can I get and save in a variable id_competicion value, for example? I try this code but it is wrong:

    $competicionid = $competicion->[competicion_id];

CodePudding user response:

getRowArray() returns an array, the correct approach to retrieve an array value would be:

$competicionid = $competicion['competicion_id'];

another possibility would be to use getRow(), which returns an object. To retrieve an object value you could use:

$competicionid = $competicion->competicion_id;

read more: Generating Query Results: Result Rows

  • Related