Skip to content
Advertisement

How to get data after @ in SQL

I have a table in the database that looks like this

id email
1 user@domain.com
2 user@domain2.com
3 user@domain3.com

I have a form that looks like this

JavaScript

In the backend I’m trying to match the input email domain with the ones in the database, after “@”.

What I’ve tried (if user enters: test@domain2.com)

JavaScript

How should I add a Query that matches $emailDomain with the database’s email domain.

I know that if I add

JavaScript

It will look for the exact email I want to look for whats after “@” in the email section

Advertisement

Answer

You already have the part after the @ stored in $emailDomain. Simply check whether the email field contains this value with the LIKE operator:

JavaScript

However, note that you cannot simply substitute value with your $emailDomain in the above query, as this would make it vulnerable to SQL injection. Instead, make sure to paramaterise the query, and use a prepared statement to avoid SQL injection:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement