Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Mar 2009
    Posts
    120
    Thanks
    13
    Thanked 3 Times in 3 Posts

    make the php code returns true if a mysql record exists...

    what i want to do is, make the php code returns true if a mysql record exists in a table, something like:

    PHP Code:
    if(record_exists($record,$table,$db) = true){echo 'yeah! true';}else{echo 'false!';} 
    how can this be done?

    by the way, i'm using php...
    Last edited by Dhuan; 04-26-2009 at 01:16 AM.

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Generally speaking, you need to run an SQL statement with specific criteria that targets that record. Something like
    Code:
    SELECT COUNT(*) FROM thetable
     WHERE thefield = 'somevalue';
    So having that as a base, your 'record_exists' function needs to take some value to check against the database, and return boolean TRUE or FALSE based on the resulting count value, e.g.
    PHP Code:
    function record_exists$table$field$criteria ) {
      
    $s"SELECT COUNT(*) FROM {$table} WHERE {$field} = '{$criteria}'";
      if ( 
    mysql_resultmysql_query($s), ) === ) {
        return 
    FALSE;
      } else {
        return 
    TRUE;
      }
    }

    // test
    if ( record_exists('usertable','userFirstName','Bob') === TRUE ) {
      echo 
    'Yes, the record exists';

    BTW, this
    PHP Code:
    if(record_exists($record,$table,$db) = true
    is using the assigment operator to attempt to make a comparison. You want to use the correct comparison operator such as '==' or '===' (which also tests type). Very common programming error, sometimes tough to spot.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •