Reputation: 877
i have a while loop from which im fetching post id and adding them to array like shown below
$tobi = array();
while($result = mysqli_fetch_array($msql)){
$imp = $result['msg_id']; // these id are like 316 etc
array_push($tobi, $imp);
}
print_r($tobi);
but when i printed the array it resulted
Array ( [0] => 316 ) Array ( [0] => 315 )
why the array has created another element? i want the array to be like
Array ( [0] => 316 [1] => 315 )
i tried using $tobi[] = $imp; also but same result got
after doing print_r($result);
Array ( [0] => 318 [msg_id] => 318 ) Array ( [0] => 318 ) Array ( [0] => 317 [msg_id] => 317 ) Array ( [0] => 317 )
after doing print_r($msql);
mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 2 [type] => 0 ) Array ( [0] => 318 ) Array ( [0] => 317 )
Upvotes: 1
Views: 81
Reputation: 2857
You're doing too much. Your SQL from the comment
$msql = mysqli_query($connecDB, "SELECT msg_id FROM messages WHERE time > $last_post_id ORDER BY time DESC")
is only getting the msg_id so when you call $imp = $result['msg_id'];
you are creating that. Hence why your Arrays were confusing.
you should be able to just do:
$tobi = array();
while($result = mysqli_fetch_array($msql)){
$tobi[] = $result[0];
}
print_r($tobi);
UPDATE
I am not sure why the odd results are being given. It seems that is outputting in ways we cannot see in the code provided. I used my database and ran:
$msql = mysqli_query($link, "SELECT f_name FROM leads");
There are 5 entries 2 do not have f_name. When I did a print_r($msql)
I received the following:
mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 5 [type] => 0 )
I then ran my code above with both numeric and associative which returned (both ways):
Array ( [0] => [1] => TEST [2] => [3] => Coury [4] => 222222 )
Also on this set I was able to run the OP original code and get the expected results.
Upvotes: 2
Reputation: 480
Try this:
$tobi = array();
while($result = mysqli_fetch_array($msql)){
if(isset($result['msg_id']))
{
$imp = $result['msg_id']; // these id are like 316 etc
array_push($tobi, $imp);
}
}
print_r($tobi);
Upvotes: 0
Reputation: 5344
Try this, I hope this will work!
$tobi = array(); $new_arr = array();
while($result = mysqli_fetch_array($msql)){
$imp = $result['msg_id'];
$new_arr[] = array_push($tobi, $imp);
}
print_r($new_arr);
Upvotes: 0
Reputation: 19
use this. it may help you.
$tobi = array();
while($result = mysqli_fetch_array($msql)){
$imp = $result['msg_id'];
$tobi[]= $imp;
}
print_r($tobi);
Upvotes: -1