I am very new to PHP. I have a function that should return a single record containing a single column, 1 or 0:
function is_pitcher($player_id) {
global $db;
$query = 'SELECT IF (position = 1, 1, 0) AS is_pitcher FROM players WHERE player_id = :player_id';
$statement = $db->prepare($query);
$statement->bindValue(':player_id', $player_id);
$statement->execute();
$is_pitcher = $statement->fetchAll();
$statement->closeCursor();
return $is_pitcher;
On my control page, I have an IF statement that evaluates $is_pitcher for values 1 and 0:
if ($action == 'get_player') {
$player_id = filter_input(INPUT_GET, 'player_id');
$is_pitcher = is_pitcher($player_id);
if ($is_pitcher == 1) {...
Testing the value with echo $is_pitcher shows that it's an array, and thus can't be evaluated this way. I have tried the function with both fetch() and fetchAll() with the same result.
I can get the value of $is_pitcher with echo $is_pitcher['is_pitcher'] but doing so on the control page breaks it.
Is there a way to write the function that does not return an array? Or, is there a way to evaluate the variable as an array?
ETA: In answer to a comment, here's the structure of the array:
array ( 0 => array ( 'is_pitcher' => '0', 0 => '0', ), )
$is_pitcher, usevar_export($is_pitcher);fetchAllalways returns an array (array can be length of 1), try$is_pitcher[0]$is_pitcher[0]['is_pitcher']- i.e.if ($is_pitcher[0]['is_pitcher'] == 1) {...