0

我有这个例子

 function read()
{
   $parameters = array();
   $results = array();

   $mysql = new mysqli('localhost', 'root', 'root', 'test') or die('There was a problem connecting to the database');
   $stmt = $mysql->prepare('SELECT id,body,posts FROM post') or die('Problem preparing query');
   $stmt->execute();

   $meta = $stmt->result_metadata(); 

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

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

   }

   call_user_func_array(array($stmt, 'bind_result'), $parameters);

   while ( $stmt->fetch() ) {
      $x = array();

      foreach( $row as $key => $val ) {

         $x[$key] = $val;
      }
      $results[] = $x;
   }

   return $results;
}

$results = read();

我想出了为什么我需要在这个例子中引用$parameters[] = &$row[$field->name]; 它没有&就行不通。我在链接中找到了这个例子。你能解释一下为什么需要使用reference 和from var $row 来自。它是 fetch_field 的一部分吗?

4

1 回答 1

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

手段$parameters[]现在正在引用,$row[$field->name]$parameters[]现在指向值,$row[$field->name]这意味着任何变化$row[$field->name]也将反映在 $parameters[]

请参阅此示例以使事情变得更好!

<?php
$foo = 'Hello';
$bar = 'World'; 
print $foo . " " . $bar;// Hello World

$foo = &$bar;
$bar = 'Hello My World';

print $foo;// Hello My World
print $bar;// Hello My World

?>

希望这可以帮助 !

于 2013-08-29T11:45:32.260 回答