0

I am confused about &reference operator. here i dont understand
the call_user_array_func() and
referense operator why used here?
but can you explain it on my code
original source is
http://net.tutsplus.com/tutorials/ph...ed-statements/
i tested it. i want to know what happens there
i know this & reference operator but for what reason this is used i cannot understand. this section

while ( $field = $meta->fetch_field() ) {

     $parameters[] = &$row[$field->name];
   }

and from where $row variable has come?

while ( $stmt->fetch() ) {
      $x = array();
      foreach( $row as $key => $val ) {
         $x[$key] = $val;
      }
      $results[] = $x;
   }

Thanks again for attention

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by twiss
0
$meta = $stmt->result_metadata();

while ( $field = $meta->fetch_field() ) {  
  
     $parameters[] = &$row[$field->name];  
   }  
  
   call_user_func_array(array($stmt, 'bind_result'), $parameters);

First, it gets the metadata of your query. Then, it gets all field names, and adds a reference to the value in the array $row with the key $field->name to $parameters.
Normally, if you would have for example SELECT id, title, body FROM posts , you would have to call $stmt->bind_result($row['id'], $row['title'], $row['body']) . But as you have those references in an array, you have to call call_user_array_func() .
Its first parameter array($stmt, 'bind_result') stands for $stmt->bind_result, and that function is than called with the parameters in $parameters , the second parameter :)

Hope that helps.

Edited by twiss: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.