I am trying to execute an SQL query to pull one column of data from my database into a PHP array, and then search for my session variable in that array. I've printed the contents of my array and it looks like the query is working and is filling the array, but my comparison if (in_array("$session", $result)) is not working correctly.
I know the string my session variable contains is inside the PHP array. But $execute never flips to FALSE. Any idea why?
$confirm = $_GET['name'];
$execute = TRUE;
session_start();
$session = $_SESSION['sessionID'];
$result = array();
try{
$conn = new PDO("mysql:host=$servername; dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(substr($session, 0, 2) === 'DS'){
$sql = $conn->prepare("SELECT confirmNum FROM `DSattendance`");
$sql->execute();
$result = $sql->fetchAll();
}
else if (substr($session, 0, 2) === 'BYOD'){
$sql = $conn->prepare("SELECT confirmNum FROM BYODattendance");
$sql->execute();
$result = $sql->fetchAll();
}
}
catch(PDOException $e){echo $sql . "<br>" . $e->getMessage();}
if (in_array("$session", $result)) {
echo "true";
$execute = FALSE;
}
if ($execute == FALSE)
echo "ALREADY REGISTERED";
var_dump($result) yields:
array(10) {
[0]=> array(2) { ["confirmNum"]=> string(11) "adfafafafaa" [0]=> string(11) "adfafafafaa" }
[1]=> array(2) { ["confirmNum"]=> string(11) "adsfafafaff" [0]=> string(11) "adsfafafaff" }
[2]=> array(2) { ["confirmNum"]=> string(11) "asdfafafafa" [0]=> string(11) "asdfafafafa" }
[3]=> array(2) { ["confirmNum"]=> string(11) "christrader" [0]=> string(11) "christrader" }
[4]=> array(2) { ["confirmNum"]=> string(11) "christradfe" [0]=> string(11) "christradfe" }
[5]=> array(2) { ["confirmNum"]=> string(11) "sadfadfafaf" [0]=> string(11) "sadfadfafaf" }
[6]=> array(2) { ["confirmNum"]=> string(11) "sadfsfafaaf" [0]=> string(11) "sadfsfafaaf" }
[7]=> array(2) { ["confirmNum"]=> string(11) "sdfsafsadfa" [0]=> string(11) "sdfsafsadfa" }
[8]=> array(2) { ["confirmNum"]=> string(11) "trraafafafa" [0]=> string(11) "trraafafafa" }
[9]=> array(2) { ["confirmNum"]=> string(11) "wesdfdfasfa" [0]=> string(11) "wesdfdfasfa" } }
var_dump($result);? Odds are it'll be returning a multi-dimensional array, thus why yourin_arrayisn't working.