Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am basically trying to fetch results from a SQL database and load that into a multidimensional array so i can use them later on in the page.

 while($row = mysqli_fetch_array($result)) 
  {

  $send = array
  (
  array($row['Name'],$row['Email'],$row['Mobile'])
  );
  $count = $count + 1;
  }

That is what i am using to get the results which if i print within the while loop it will echo all the results. However when putting it into the array it loads each result into the array as the first result. My initial plan was to use a counting variable to set where in the array the result was set to with this adding by one each time. I am not certain how to specify where to add the result i thought something along the lines of

$send = array[$count]
(
array.....

so i could then refer to the results as 0 to count length but i am not sure how to make this work. Or ,which i presume, if there is a much easier and better way of going about it. I am also not sure if this is necessary as surely the results seem to be in an array when gathered from the SQL database but i am unsure if this array is populated with each while loop or stored and can be accessed at any point

If any one can give me an example of something similar or point me at some documentation much appreciated

share|improve this question

3 Answers 3

Try this:

$count = 0;
while ($row = mysqli_fetch_array($result)) {
    $send[$count] = array($row['Name'], $row['Email'], $row['Mobile']);
    $count++;
}

I have a better way for you. You could also use the id for your index, if you have one:

while ($row = mysqli_fetch_array($result)) {
    $send[$row['id']] = array(
        "Name"      => $row['Name'], 
        "Email"     => $row['Email'], 
        "Mobile"    => $row['Mobile']
    );
}
share|improve this answer

You can use:

$count = 0;
while($row = mysqli_fetch_array($result)) 
{
    $send[$count] = $row;
    $count ++;
}

Also you might want to use the table id as an array index, so you can access the records by ID later. In that case you can do:

while($row = mysqli_fetch_array($result)) 
{
    $send[$row['id']] = $row;
}
share|improve this answer

You're declaring your array inside your loop. So it will reset it every time.

$send = array();
while($row = mysqli_fetch_array($result)) 
  {
  $send[] = array($row['Name'],$row['Email'],$row['Mobile']);
  }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.