Query Database Topic is solved

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
jonmd
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Jan 28, 2019 3:12 pm

Query Database

Post by jonmd » Mon Jan 28, 2019 3:20 pm

Hello all, I am trying to create a database query to check if an email address exists in a table. Here's the code I have currently:

Code: Select all

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('email');
$query->from($db->quoteName('#__com_registrants'));

$query->where($db->quoteName('email') . " = " . $db->quote('[email protected]'));

// Reset the query using our newly populated query object.
$db->setQuery($query);

// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();

print_r($results);
The "[email protected]" email address exists in my table, so running the code as-is works correctly. What I'm looking to do is code this so that it will pull based on the email domain, not an exact user. If I re-run the query as:

Code: Select all

$query->where($db->quoteName('email') . " LIKE " . $db->quote(''\'gmail.com%\'''));
I do not get any results. What am I missing here?

jonmd
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Jan 28, 2019 3:12 pm

Re: Query Database

Post by jonmd » Mon Jan 28, 2019 3:40 pm

I updated my query to this which did work:

Code: Select all

$query->where($db->quoteName('email') . ' LIKE ' . $db->quote($db->escape('%gmail%')));

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 30814
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Query Database

Post by Per Yngve Berg » Mon Jan 28, 2019 3:40 pm

Match any user on the domain:

Code: Select all

$query->where($db->quoteName('email') . " LIKE " . $db->quote('%@gmail.com'));
Match also sub-domains:

Code: Select all

$query->where($db->quoteName('email') . " LIKE " . $db->quote('%gmail.com'));


Locked

Return to “Joomla! 3.x Coding”