1

MySQL table (allow_ip)

username |      ip
  @@@    |192.215.154.251
         |
         |
         |

I want to take the all the ip and create array but dont work!

$rs=$mysqli->query('SELECT ip FROM allow_ip');
$i=0;
while($row = $rs->fetch_assoc())
{
    $allow[$i]=$row['ip'];
    $i++;
}

I want this: $allow = array("ip[0]", "ip[1]",....,"ip[x]");

3 Answers 3

3

Is this what you mean?

$arrDbEntry = $mysqli->query("SELECT ip FROM allow_ip");

$arrAllow = array();

while($intRow = $arrDbEntry->fetch_assoc()) {
    $arrAllow[] = $arrDbEntry['ip'];
}

// print_r($arrAllow);

Although, I think if you increment $i each time in your loop, that should do the trick. You could also create a small function, call it something like selectAndFetchAll($strDbQuery, $arrParameters) and make it return an array of results.

Sign up to request clarification or add additional context in comments.

Comments

0

before the while loop, declare $allow as array: $allow = array(); and inside the loop, increese $i: $i++;

3 Comments

Ok work, but now i have other problem! I have login system and i do this: $rs=$mysqli->query('SELECT ip FROM allow_ip'); $allow = array(); $i=0; while($row = $rs->fetch_assoc()) { $allow[$i]=$row['ip']; $i++; } if(!in_array($_SERVER['REMOTE_ADDR'], $allow) && !in_array($_SERVER["HTTP_X_FORWARDED_FOR"], $allow)) { header("Location: ../../404.php"); exit(); } Work but..... Dont work logout!
please consider opening a new question for this
you need to call session_start(); and session_destroy(); inside your if condition.
0
  public function selectInfo($columnName,$table){
      include('../includes/db_connect.php'); 
      $select = "SELECT $columnName FROM $table";
      $selected = $db->query($select);
      while($rows = mysqli_fetch_array($selected)){
      print_r(array_keys($rows));
      }
  }

Then, just build it into your class - if your using one...

    $showInfo = new dbFunction();
    $showInfo->selectInfo('Column Name','Your Table');  

or just the function...

    selectInfo('Column Name','Your Table');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.