Becky Becky - 1 year ago 81
SQL Question

Skip results inside a foreach loop - pdo

In my database I store email addresses. In the foreach loop how can I skip all email addresses that has

$sql = "SELECT * FROM myMovieCustomers";
$stmt = $pdo->prepare($sql);
$result = $stmt->fetchAll();

foreach ($result as $row) {
//skip if $row['email'] contains{
//do someting with these

Answer Source

it's more efficient to do filtering at the database level for a couple of reasons

  • you're not wasting network resources transmitting unneeded data across the wire
  • with a well indexed table, the DBMS will be faster at making the search than php will with the result data

as is pointed out in the comments and other answers, you can use the LIKE operator

$sql = "SELECT * FROM myMovieCustomers WHERE email NOT LIKE ''";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download