• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

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

0
Bateman
Asked:
Bateman
1 Solution
 
husker475Commented:
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

0
 
NoiSCommented:
Seems its all right

Try this way
if(recordExists(fld_email,$email,tbl_users,$db)>0){
  echo 'The record exist';
}else
{
  echo 'The record doesn't exist';
  // stuff here
}

Open in new window

0
 
BatemanAuthor Commented:
many thanks, seems to work!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now