user2977729
user2977729

Reputation: 145

How to compare PHP variable to value from PHP array

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" } }

Upvotes: 2

Views: 1366

Answers (1)

RhapX
RhapX

Reputation: 1683

The PDO Statement fetchAll is returning a multi-dimensional array of the results found in the database. You need to loop through them first and format a new array that you can use to check your session against.

foreach($result as $value) {
    $array[] = $value['confirmNum'];
}

if( in_array($session, $array)) {
    // your code here
}

As noted by @mr12086, depending on the version of PHP you are using, you can avoid the foreach loop by using: $result = array_column('confirmNum', $result); instead. However, this does require PHP 5.5.0 or higher.

Upvotes: 2

Related Questions