2

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" } }
1
  • What is the output of var_dump($result);? Odds are it'll be returning a multi-dimensional array, thus why your in_array isn't working. Commented Aug 9, 2015 at 23:14

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Depending on php version you could replace the loop with $result = array_column('confirmNum', $result); for less code.
Absolutely @mr12086, thanks for adding. I updated my answer to include your response as well. Credit given :)
@user2977729 np, glad to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.