/ Published in: PHP
A function that I use from time to time in order to make sure that a user name or something similar is not already in the database. Mainly just for user names though.
Expand |
Embed | Plain Text
// FUNCTION isUnique // // $table -> table to check // $field -> column to check // $value -> value to check // $except -> a clause to add to the mix. // // ex if( ! isUnique( "users" , "user_login" , "user1" ); // ex if( ! isUnique( "users" , "user_login" , "user1" , "user_id != XX" ); function isUnique( $table , $field , $value , $except = null ) { // build the query // value is escape cause maybe that comes from the user form // the rest should be coming from the programmer. $q = "SELECT COUNT( ".$field." ) as total FROM ".$table." WHERE ".$field." = '".mysql_escape_string( $value )."'"; // ok exception clause so lets just add it to the where // this probably gets used when a user does an update to their // user name... if( $except != null ) { $q .= "' AND ".$except; } $qr = dbquery( $q ); $total = $qr->fields['total']; return $total == 0; }
You need to login to post a comment.
