phparrayspdoarray-key-exists

Check if a record exists in an array using PDO and Mysql


I'm trying to get a record in my mysql whether a record exists or not in an array. I've searched online about using array_key_exists function and unfortunately it always gives me "Match not found" even though the record exists in the array.

This is my output.

Array ( [0] => Array ( [0] => Array ( [GUESTCHECK] => 10017 [0] => 10017 ) [1] => Array ( [GUESTCHECK] => 10024 [0] => 10024 ) [2] => Array ( [GUESTCHECK] => 10066 [0] => 10066 ) [3] => Array ( [GUESTCHECK] => 10084 [0] => 10084 ) [4] => Array ( [GUESTCHECK] => 10150 [0] => 10150 ) [5] => Array ( [GUESTCHECK] => 10154 [0] => 10154 ) [6] => Array ( [GUESTCHECK] => 10223 [0] => 10223 ) [7] => Array ( [GUESTCHECK] => 10224 [0] => 10224 ) [8] => Array ( [GUESTCHECK] => 10225 [0] => 10225 ) [9] => Array ( [GUESTCHECK] => 10261 [0] => 10261 ) [10] => Array ( [GUESTCHECK] => 10284 [0] => 10284 ) [11] => Array ( [GUESTCHECK] => 10298 [0] => 10298 ) [12] => Array ( [GUESTCHECK] => 20012 [0] => 20012 ) [13] => Array ( [GUESTCHECK] => 20014 [0] => 20014 ) [14] => Array ( [GUESTCHECK] => 20054 [0] => 20054 ) [15] => Array ( [GUESTCHECK] => 20056 [0] => 20056 ) [16] => Array ( [GUESTCHECK] => 20061 [0] => 20061 ) [17] => Array ( [GUESTCHECK] => 20131 [0] => 20131 ) [18] => Array ( [GUESTCHECK] => 20160 [0] => 20160 ) [19] => Array ( [GUESTCHECK] => 20163 [0] => 20163 ) [20] => Array ( [GUESTCHECK] => 20183 [0] => 20183 ) ) ) 

Help me by telling what's wrong with my code?

  $gchecksummary = "SELECT DISTINCT GUESTCHECK FROM `items` 
where CODE = 771012 and DATE = '1/1/2018' or CODE = 771013 
and DATE = '1/1/2018' or CODE = 782012 
and DATE = '1/1/2018' or CODE = 774012 
and DATE = '1/1/2018' or CODE = 775203 
and DATE = '1/1/2018' or CODE = 775202 and DATE = '1/1/2018'";
$execgchecksummary  = $conn->prepare($gchecksummary);
$execgchecksummary->execute();


while($fetchgchecksummary = $execgchecksummary->fetchAll()){

$results[] = $fetchgchecksummary;

} 


 print_r($results);
 echo "<br />";

 if (array_key_exists("10017", $results))
  {
  echo "Match found";
  }
else
  {
  echo "Match not found";
  }

Solution

  • Problem is in Your code logic and sql query.

    Why to take array of unnecessary rows and do lookup in it?

    Also You're repeating DATE condition with same value multiple times

    You can just define condition for GUESTCHECK inside Your query, then check result count and do fetchAll (if there's a need on data).

    Example with rows data:

      $query = "
        SELECT * 
        FROM `items` 
        WHERE 
         GUESTCHECK = 10017 
          AND
         CODE IN (771012, 782012, 774012, 775203, 775202) 
          AND
         DATE = '1/1/2018'
      ";
    
      $statement = $conn->prepare($query);
      $statement->execute();
    
      if ($statement->rowCount() > 0)
      {
        echo "Match found";
    
        $items = $statement->fetchAll();
        var_dump($items);
      }
      else
      {
        echo "Match not found";
      }
    

    Example without getting rows from db:

      $query = "
        SELECT 1
        FROM `items` 
        WHERE 
         GUESTCHECK = 10017 
          AND
         CODE IN (771012, 782012, 774012, 775203, 775202) 
          AND
         DATE = '1/1/2018'
        LIMIT 1
      ";
    
      $statement = $conn->prepare($query);
      $statement->execute();
    
      if ($statement->rowCount() > 0)
      {
        echo "Match found";
      }
      else
      {
        echo "Match not found";
      }
    

    p.s. make sure You've added index to GUESTCHECK and CODE fields to help DB engine to search for data effectively.