Guest User

Untitled

a guest
Apr 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. <?php
  2.  
  3. $fruit_names = [];
  4.  
  5. $stmt = $dbh->prepare('SELECT id, fruit_name FROM fruits');
  6. if ($stmt->execute()) {
  7. $result = $stmt->fetchAll();
  8. $result_count = count($result);
  9. if ($result_count > 0) {
  10. foreach($result as $row) {
  11. // decrypt and push into array
  12. $cur_fruit_name = $row['fruit_name'];
  13. try {
  14. $decrypted_fruit_name = Crypto::decrypt($cur_fruit_name, $user_key);
  15. $fruit_names[$row['id']] = $decrypted_fruit_name;
  16. }
  17. catch (Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException $ex) {
  18. // Either there's a bug in our code, we're trying to decrypt with the
  19. // wrong key, or the encrypted credit card number was corrupted in the
  20. // database.
  21.  
  22. // ... handle this case ...
  23. $fruit_names[$row['id']] = "failed to decrypt";
  24. }
  25. }
  26. }
  27. else {
  28. // empty result handler
  29. }
  30. }
  31.  
  32. // then you can search for what you originally wanted
  33. $requested_fruit_name = $_POST['requested_fruit']; // say apple
  34. $match_found = false;
  35.  
  36. foreach ($fruit_names as $fruit_id => $fruit_name) {
  37. if ($fruit_name == $requested_fruit_name) {
  38. $match_found = true;
  39. return $fruit_id;
  40. break;
  41. }
  42. }
  43.  
  44. if (!$match_found) {
  45. return 'match not found';
  46. }
Add Comment
Please, Sign In to add comment