Reputation: 4333
I need to get all the values in one column in a table so I can create a whitelist for another query. Here's how I'm getting that:
$stmt = $dbh->prepare("select GROUP_CONCAT( cohort_id SEPARATOR ',') as cohort_id from ( select distinct cohort_id from table_cohorts) as m");
$stmt->execute();
$row = $stmt->fetch();
$avails = json_encode($row);
If I var_dump
$avails
, I get this:
string(125) "{"cohort_id":"national_percent,database_percent,cohort_1,cohort_2","0":"national_percent,database_percent,cohort_1,cohort_2"}"
The stuff with "cohort_id" is what I want. I need to form an array so I can drop it in something like this:
(in_array($sortvalue, $arrayofpossibleoptions))
//$sortvalue
comes from AJAX
How can I get this in the right format? One further wrinkle, I need to add one additional value to the $arrayofpossibleoptions
or something like this, but not sure what the proper syntax is:
(in_array($sortvalue, $arrayofpossibleoptions || 'another' ))
If I var_dump
$row
, I get this:
array(2) {
["cohort_id"]=>
string(51) "national_percent,database_percent,cohort_1,cohort_2"
[0]=>
string(51) "national_percent,database_percent,cohort_1,cohort_2"
}
Upvotes: 1
Views: 620
Reputation: 41903
If you need this data to be loaded into in_array
, then it should be in an array form, you can't use a json string.
$stmt = $dbh->prepare('SELECT DISTINCT cohort_id FROM table_cohorts');
$stmt->execute();
$cohort_ids = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$cohort_ids[] = $row['cohort_id'];
}
if(in_array($user_input, $cohort_ids)) {
// do the rest
} else {
// oops your input is not any of those
}
Upvotes: 1