Link to home
Start Free TrialLog in
Avatar of Bateman
Bateman

asked on

recordExists Function - correct use

Hi, I am starting to use a recordExists Function (shown below) to check for existing records in a database before adding a new one. Could someone help me out with the correct way in using this:

I tried the following but it didn't seem to work:

recordExists(fld_email,$email,tbl_users,$db);
if (recordExists == "1");
else
{
//stuff here
}
<?
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 function recordExists
?>

Open in new window

Avatar of husker475
husker475
Flag of Sweden image

Use mysql_num_rows

http://se.php.net/mysql_num_rows

if(mysql_num_rows($result) > 0){
//Record exists
}
else{
//No record with that id exists
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of NoiS
NoiS
Flag of Brazil image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bateman
Bateman

ASKER

many thanks, seems to work!