DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Record Exists

07.07.2005
| 24989 views |
  • submit to reddit
        In mySQL (from PHP), checks for the existance of a record based on id, passing the name of the id field followed by the id to check for, the table name, and the database resource, in that order.
function recordExists($id,$idval,$table,$db) {//check for id=idval in table and return TRUE or FALSE
   $result = mysql_query("SELECT * FROM ".$table." WHERE ".$id."='".$idval."'", $db) or die(mysql_error());
   if($row = mysql_fetch_array($result)) {//if we did return a record
      return 1;
   }//end if row
   return 0;
}//end fuction recordExists