How can I check whether a field from a table is set as UNIQUE?
For example I have a table named users
with a field email
set as UNIQUE
and a field picture
not set as UNIQUE
, I want before selecting check whether the field is set set as UNIQUE
if not then don't do the SELECT
.
I tried to SELECT
then count the returned number of row, if more than 1 then it's not UNIQUE
,
"SELECT * FROM table WHERE email='$email'"
//...some mysql php line later
if($count > 1){
//return nothing
}
but it's not efficient, what if there is no duplicate.
What's the best way to check whether a field is set as UNIQUE
in PHP
?
Edit: no duplicate doesn't mean it has UNIQUE
property
phpmyadmin
– Reredosusername=bob
[email protected]
but if the username is not unique then the user could not do a search by username and I'd throw an error. – Orientate