Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

arecuk's avatar

need help with building a sql query

Hi guys!

Im struggling with little bit complicated sql query:

public function isInHaplotype ('phenotype','10', $rs_ID_needle,'In Panel') {

    $getHaplotypes = $ghcCore->table('haplotypes')->select('id',
                                               'rs_id_1',
                                               'rs_id_2',
                                               'rs_id_3',
                                               'rs_id_4',
                                               'rs_id_5',
                                               'risk_allele',
                                               'odds_ratio')
                                      ->where($cond,$id)->where('status',$status)
                                      ->where(function($query){
                                              $query->where('rs_id_1',$rs_ID_needle);
                                                    //->orWhere('rs_id_2',$rs_ID_needle)
                                                    //->orWhere('rs_id_3',$rs_ID_needle)
                                                    //->orWhere('rs_id_4',$rs_ID_needle)
                                                    //->orWhere('rs_id_5',$rs_ID_needle);
                                             })
                                      ->count();

When I run this query I get: "Undefined variable: rs_ID_needle"

Meaning I can not pass the function parametr $rs_ID_needle to the query.

When I hard code the parametr the query starts to work just fine.

So basically I need to pass the variable to this part of the code: ->where(function($query){ $query->where('rs_id_1',$rs_ID_needle);

And thats what does not work and I get "Undefined variable: rs_ID_needle"

0 likes
2 replies
matthew_inamdar's avatar
Level 4
->where(function($query) use ($rs_ID_needle) {
    $query->where('rs_id_1', $rs_ID_needle);
    //->orWhere('rs_id_2',$rs_ID_needle)
    //->orWhere('rs_id_3',$rs_ID_needle)
    //->orWhere('rs_id_4',$rs_ID_needle)
    //->orWhere('rs_id_5',$rs_ID_needle);
})
                                                

Try this

1 like

Please or to participate in this conversation.