Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
publicGenome
Forum Contributor
Posts: 110 Joined: Thu Apr 16, 2015 7:55 am
Post
by publicGenome » Wed Dec 09, 2015 3:13 pm
Hi There,
I'm stuck while trying to check if row with specific value is present in a column or not.
Code: Select all
//$sample_group_name - is some value
$row=DB::connection('illumina')->table('sample_group')->where('sample_group_name',$sample_group_name)->get();
if(is_null($row)){
//doesn't work if rows are null
Log::info($row); //null
Log::info(" hahahah ");
$present=false;
}
else{
Log::info($row); //null
Log::info("bbabababa");
$present=true;
}
However, code below works fine :
Code: Select all
$row=DB::connection('illumina')->table('sample_group')->where('sample_group_name',$sample_group_name)->first();
if(is_null($row)){
//works if rows are null
Log::info(" hahahah ");
$present=false;
}
else{
Log::info("bbabababa");
$present=true;
}
Laravel 4.2
Both these queries are run in Model file.
I wasted an hour to figure out what's going on.
Please guide.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Dec 09, 2015 3:42 pm
Okay, so it's not null. Then what is the value of $row?
publicGenome
Forum Contributor
Posts: 110 Joined: Thu Apr 16, 2015 7:55 am
Post
by publicGenome » Wed Dec 09, 2015 3:53 pm
Hi requinix,
Thanks for your reply.
When it's null:
Code: Select all
$row=DB::connection('illumina')->table('sample_group')->where('sample_group_name',$sample_group_name)->get();
array (
)
With above same query, if present:
array (
0 =>
array (
'id' => 2,
'user_id' => 'abc_',
'sample_count' => 8,
'sample_group_name' => 'asdsad',
'sample_name' => '',
'created_at' => '2015-12-09 16:02:03',
'updated_at' => '2015-12-09 16:02:03',
),
--------
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Dec 09, 2015 4:04 pm
Alright. So you see what kind of value you have to work with. Which is not null, to be clear about it. Do you have any ideas about how you might test for that value?
publicGenome
Forum Contributor
Posts: 110 Joined: Thu Apr 16, 2015 7:55 am
Post
by publicGenome » Wed Dec 09, 2015 4:28 pm
See if the length of the array is 0 or above?
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Wed Dec 09, 2015 5:11 pm
Since it always returns an array, simpler would be:
Code: Select all
$row=DB::connection('illumina')->table('sample_group')->where('sample_group_name',$sample_group_name)->get();
if ($row) {
Log::info($row); //null
Log::info("bbabababa");
$present=true;
} else {
//doesn't work if no rows
Log::info($row); //null
Log::info(" hahahah ");
$present=false;
}
Plus, I think it is good practice to have the success condition be first in the if()'s and the else be the error condition.
(#10850)
publicGenome
Forum Contributor
Posts: 110 Joined: Thu Apr 16, 2015 7:55 am
Post
by publicGenome » Thu Dec 10, 2015 11:26 am
Hi Chris,
Thanks for your reply.