1

I'm trying to retrieve data from my table and save all the results in an array.

$query = " SELECT id,sent_to FROM `table` WHERE `ac` = 0 AND `cut` < CURRENT_DATE() ";
$result = mysqli_query($con,$query);
$sentToList = array();
while($row = mysqli_fetch_assoc($result)) { $sentToList = $row['sent_to']; }
print_r($sentToList); // to check if the results have been saved in the variable

$sent_to = explode(',', $sentToList); //for my next step

When I'm printing $sentToList I'm getting only the last result of the query. There are similar questions answered but I didn't quite get the explanation. Thank you.

2
  • This will be even cleaner and simpler while($row[] = mysqli_fetch_assoc($result)) { } Commented Jun 30, 2017 at 13:36
  • @MilanChheda I don't understand why, but I'll keep that in mind. Thank you for mentioning it =) Commented Jun 30, 2017 at 13:37

1 Answer 1

9

$sentToList = $row['sent_to']; this will save value of $row['sent_to']; in variable. So this will update $sentToList every time

Try this

$sentToList[] = $row['sent_to'];

this will assign value into array

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

1 Comment

So my code created a new variable that is not an array called $sentToList instead of using the array variable create on the line before? it worked by the way haha.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.