Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Illogical catch all

So I am having to write some PHP to run some SQL and manage a few things in a database. Mostly checking for invalid emails in the email column in the database and then for handling duplicate emails in the database.

I'm on a loop now for checking if there are duplicate emails comparing them with logins associated.

On this loop currently I have an if/else statment thats is checking the login count if0 do X if 1 do X if >1 do X. I then though I ought to put a catch all in there as a final else statment which would fire off only if the returned valid is not 0 not 1 and not greater than 1 which would be illogical but should I have it?

foreach ($emails as $key => $email) {
  if (count($email["login"]) = 0) {

  }
  } else if (count($email["login"]) = 1) {

  } else if (count($email["login"]) > 1) {

  } else {
    // Is NOT 0, 1 or >1... there is a logic error here.
  }
}

The end product for those curious is something like this

// connect to database
  // sql: get all emails from user_account

  // check if emails are invalid
    // if invalid set them to empty string
    // else (if valid) continue

  // sql: write changes back to database
// close database connection


// connect to database
  // sql: find duplicate emails

    // if more than 1 duplicate emails has a login associated, 
      // log it for admin

    // if only one of the duplicate emails has a login
      // set duplicates without login back to empty string
      // duplicate with login leave alone and continue

    // if no duplicate emails has a login find the one that was modified last
        // if not modified last set to empty string
        // else continue

   // sql: write changes back to database
// close connection

I think I have Andrew Chalkley's 'Plan' stage figured out

1 Answer

Jeff Lemay
Jeff Lemay
14,268 Points

It seems like it would be good practice to have a catchall here, just in case something goes haywire. Could be something simple that states "We encountered an error. Please try again."

Alright. Boss threw this task at me to get me more privvy with SQL and PHP interacting with SQL. I get the logic... now its just a matter of making it work.