0

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

3 Answers 3

0

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']
    );
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

Comments

0

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

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.