Mysql data to php array

Discussion in 'PHP' started by red-x, Oct 29, 2009.

  1. #1
    Hi, I'm stuck trying to get data from Mysql to a php array().

    I'm trying to do a script that bans IPs. At first my array was like this..

    $ips = array('127.0.0.1', '127.0.0.2', '127.0.0.3'); //etc.
    PHP:
    and then the script will check it like this...

       
    
    $user_ip = $_SERVER['REMOTE_ADDR'];
    
     if(in_array($user_ip, $ips))
    	{
    	  die("Banned!");
    }
    
    
    PHP:
    But now I have everything on a mysql table, so how do I check them? I had..

      $sql_ban = "SELECT * FROM banned_users WHERE ip";
      $result_ban = mysql_query($sql_ban) or die(mysql_error());
      $row_ban = mysql_fetch_array($result_ban);
      $ban_ip = $row_ban["ip"];
      $ban_username = $row_ban["username"];
    
    PHP:
    and then I will just do..

    $ips = array($ban_ip);
    PHP:
    But that only works with the first IP. I trying adding a while() but that only works with the last IP :confused: .. any help will be useful thank you in advance. :)
     
    red-x, Oct 29, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    while(row etc){
    array_push ($array, $row-value)
    }
    got the idea?
     
    crivion, Oct 29, 2009 IP
  3. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #3
    Why don't you use database for it's real purpose?
    
    $user_ip = $_SERVER['REMOTE_ADDR'];
    $sql_ban = "SELECT * FROM banned_users WHERE ip = '".$user_ip."'";
    $result_ban = mysql_query($sql_ban) or die(mysql_error());
    if($row_ban = mysql_fetch_array($result_ban)){
      // this ip is banned
      die("your ip is banned");
    }
    
    PHP:
     
    AsHinE, Oct 29, 2009 IP
  4. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    AsHinE is right.
     
    plog, Oct 29, 2009 IP
  5. red-x

    red-x Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Awesome thanks a lot :D
     
    red-x, Oct 31, 2009 IP
  6. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Why don't you use PHP for it's real purpose?
    Also changed query to narrow returned data...

    $user_ip = $_SERVER['REMOTE_ADDR'];
    $sql_ban = "SELECT ip FROM banned_users WHERE ip = '".$user_ip."' LIMIT 1";
    $result_ban = mysql_query($sql_ban) or die(mysql_error());
    if(mysql_num_rows($result_ban)){
      // this ip is banned
      die("your ip is banned");
    }
    PHP:
     
    NatalicWolf, Nov 1, 2009 IP
  7. tonythetiger

    tonythetiger Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I see this common mistake all the time and thought i should offer a correction. IP addresses saved in db's should ALWAYS be saved as numeric values reasons for this include but are not limited to better and more efficient indexing and faster operand comparisons. With that said assuming you have all your ip columns as integers, the below code is what you need. the INET_ATON() mysql function converts ip to numeric value and the INET_NTOA function allows you to go from numeric to ip address.

    
    $user_ip = $_SERVER['REMOTE_ADDR'];
    $sql_ban = "SELECT * FROM banned_users WHERE ip = INET_ATON('".$user_ip."')";
    $result_ban = mysql_query($sql_ban) or die(mysql_error());
    if(mysql_num_rows($result_ban) > 0){
      // this ip is banned
      die("your ip is banned");
    }
    Code (markup):
     
    tonythetiger, Nov 1, 2009 IP