recordExists Function - correct use

Posted on 2008-11-17
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:

if (recordExists == "1");
//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

Question by:Bateman
    LVL 4

    Expert Comment

    Use mysql_num_rows

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

    Open in new window

    LVL 11

    Accepted Solution

    Seems its all right

    Try this way
      echo 'The record exist';
      echo 'The record doesn't exist';
      // stuff here

    Open in new window


    Author Closing Comment

    many thanks, seems to work!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    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.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now