I need to check whether a value is in a DB. I am using
$db = JFactory::getDBO();
$query = $db->getQuery( true );
$query->select($db->quoteName('id'));
$query->from($db->quoteName('#__tablename'));
$query->where($db->quoteName('fieldname') . ' = ' . $db->quoteName($valuetocheck));
$db->setQuery($query);
$id = $db->loadResult();
echo "value = " . $id;
if($id) {/*action if the value already exists*/} else {/*...*/}
The problem is that every new value that I set to $valuetocheck
is actually being inserted into the table. If I set a value that does not exist yet, it gets inserted, and so I always get a valid $id
and cannot proceed to the else {}
.
Is that possible? Is there any workaround to check whether a value is already inside?