$arr[]=$data doesnt add the $data

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

mash
New php-forum User
New php-forum User
Posts: 7
Joined: Sat Jan 14, 2012 1:36 pm

$arr[]=$data doesnt add the $data

Postby mash » Thu Oct 03, 2013 4:41 am

Hi,
I am fetching rows from a table with a prepared statement and try to add the resulting rows
into an array.
Everything works fine - if I output the data of each fetch operation it shows the correct data.

BUT I want to return the collected data in an array - like:

Code: Select all

$metaResults = $this->connlink->result_metadata();
$fields = $metaResults->fetch_fields();
foreach($fields as $field){
   $rows[]=&$field->name;
}
call_user_func_array(array($this->connlink,'bind_result'), $rows);
$count=0;

while(($this->connlink->fetch())&&($count<2)){

   yecho($rows,'Row: '.$count);
   $result[]=$rows;
  $count++;
}

So after the while loop has finished $result contains 'n' times the last recordset that has been fetched but not the first ones... what is wrong?
The same happens when trying it with array_push()

Edit: to be more precise: The $result - Array contains the correct amount of Data (i.e. the amount of rows selected), but every recordset in the $result-Array contains the data of the last recordset retrieved.
thanks in advance

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: $arr[]=$data doesnt add the $data

Postby johnj » Fri Oct 04, 2013 12:46 am

you can do two things:

1). Initialize the array outside the while loop using $result = array()

2).Inside the while loop, please print what you storing..e.g. print_r($rows);

mash
New php-forum User
New php-forum User
Posts: 7
Joined: Sat Jan 14, 2012 1:36 pm

Re: $arr[]=$data doesnt add the $data

Postby mash » Fri Oct 04, 2013 1:56 am

johnj wrote:you can do two things:

1). Initialize the array outside the while loop using $result = array()

2).Inside the while loop, please print what you storing..e.g. print_r($rows);


1.) I have initialized the array (just didn't have it in the example code).

2.) if I add e.g. the numbers from 1..10 the array has the values:
first run:
$rows = 1;
2nd run:
$rows =2,2
3rd run:
$rows =3,3,3
...
instead of
first run:
$rows = 1;
2nd run:
$rows =1,2
3rd run:
$rows =1,2,3

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: $arr[]=$data doesnt add the $data

Postby johnj » Fri Oct 04, 2013 8:35 pm

1). what function is this yecho ?

2).please post the full code.

mash
New php-forum User
New php-forum User
Posts: 7
Joined: Sat Jan 14, 2012 1:36 pm

Re: $arr[]=$data doesnt add the $data

Postby mash » Sat Oct 05, 2013 3:13 am

yecho is just something like r_print (with more features) to debug contents.
sorry at the moment I can not post the full code at least not extract the essential parts to some working demo - due to lack of time... maybe tomorrow


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

cron