petrosvel
petrosvel

Reputation: 13

Create array from mysqli

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]");

Upvotes: 1

Views: 113

Answers (3)

PMaki
PMaki

Reputation: 1

  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');

Upvotes: 0

Tinlozara
Tinlozara

Reputation: 76

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.

Upvotes: 3

Alon Eitan
Alon Eitan

Reputation: 12025

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

Upvotes: 0

Related Questions