I need to check if a record exists or not in php zend.
I have the following sql
query which works fine and gives the proper results
(SELECT * FROM email where isLogged = 1 and emailId='demo@xyz.com')
UNION
(SELECT * FROM email where idPeople = 5 and emailId='demo@xyz.com');
I want to write this query using Zend_Validate_Db_RecordExists
to check if record with email= 'demo@xyz.com'
exists or not under said conditions.
I don't know why you would use a UNION for this, the following code should do the job:
$email = 'demo@xyz.com';
$clause = $dbAdapter->quoteIdentifier('isLogged') . ' != 1 OR ' . $dbAdapter->quoteIdentifier('idPeople') . ' != 5';
$validator = new Zend\Validator\Db\RecordExists(
array(
'table' => 'email',
'field' => 'emailId',
'adapter' => $dbAdapter,
'exclude' => $clause
)
);
if ($validator->isValid($email)) {
// username appears to be valid
} else {
// username is invalid; print the reason
$messages = $validator->getMessages();
foreach ($messages as $message) {
echo "$message\n";
}
}
The code excludes records that either have isLogged != 1
or idPeople != 5
, please let me know if I misunderstood.
Edit: I got curious about UNION vs OR performance and found this link explaining some of the details: https://stackoverflow.com/a/13866221/3784145
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.