?
Solved

recordExists Function - correct use

Posted on 2008-11-17
3
Medium Priority
?
231 Views
Last Modified: 2012-05-05
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
Comment
Question by:Bateman
3 Comments
 
LVL 4

Expert Comment

by:husker475
ID: 22979929
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
 
LVL 11

Accepted Solution

by:
NoiS earned 2000 total points
ID: 22979948
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
 

Author Closing Comment

by:Bateman
ID: 31517631
many thanks, seems to work!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question