Need help with short php/mysql code

Discussion in 'PHP' started by Crayz, Feb 3, 2007.

  1. #1
    Hello, I was wondering how I would go about looking up information in a MySQL database inside an if statement, and if it exists, skip the functions inside the if function.

    Like,

    if (mysql data found = true) {
    code
    }


    Thanks!

    Edit: The thing being looked for in the database isnt specific, it would be like, say, a visitor came to your website last night and his/her ip address was logged. Then when he/she comes again today, I would need to check that via the code I am trying to get, and perform some code if its found.
     
    Crayz, Feb 3, 2007 IP
  2. monkey56657

    monkey56657 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $Q = mysql_query("SELECT * FROM `tablenamehere` WHERE `ip` = '".$_SERVER['REMOTE_ADDR']."' LIMIT 1");
    
    if ($Q){
    
    //blabla
    
    };
    
    PHP:
    something like that should work for u...change colum and table names to fit.
     
    monkey56657, Feb 3, 2007 IP
  3. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you will want to do this:

    
    $sql = "[your sql statement here]";
    $rst = mysql_query($sql);
    
    if(mysql_num_rows() > 0) {
      // code if found
    }
    else {
      // code if not found
    }
    
    Code (markup):
     
    daboss, Feb 3, 2007 IP
  4. Crayz

    Crayz Well-Known Member

    Messages:
    708
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    120
    #4
    Ok cool, thanks.
     
    Crayz, Feb 3, 2007 IP
  5. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ok - made a small mistake ;)

    corrected as below:

    
    $sql = "[your sql statement here]";
    $rst = mysql_query($sql);
    
    if(mysql_num_rows([COLOR="Red"][B]$rst[/B][/COLOR]) > 0) {
      // code if found
    }
    else {
      // code if not found
    }
    
    Code (markup):
     
    daboss, Feb 3, 2007 IP