在下面的代码中,有人能告诉我变量 $row 在 foreach 语句中来自哪里吗?
public function getProcResultSet($cmd)
{
try
{
$meta = $cmd->result_metadata();
while ($field = $meta->fetch_field())
{
$params[] = &$row[$field->name];
}
call_user_func_array(array(
$cmd,
'bind_result'), $params);
while ($cmd->fetch())
{
foreach ($row as $key => $val)
{
$c[$key] = $val;
}
$results[] = $c;
}
return $results;
}
catch (Exception $e)
{
logToFile("Exception: " . $e);
return resultFailedUnknown();
}
}
编辑,这里是这个函数的调用者:
public static function getPlayerUnits($playerId, $unitTypeId)
{
$mysqli = new mysqli(GDB_HOST, GDB_USERNAME, GDB_PASSWORD, GDB_NAME);
if ($mysqli->connect_errno)
{
throw new Exception('DB Connection Failed. Error Code: ' . $mysqli->connect_errno);
}
$cmd = $mysqli->prepare('CALL sp_get_player_units(?, ?)');
if (!$cmd)
{
throw new Exception($mysqli->error);
}
$cmd->bind_param('ii', $playerId, $unitTypeId);
$cmd->execute();
$results = parent::getProcResultSet($cmd);
$cmd->close();
$mysqli->close();
return $results;
}
这是我所做的只是在客户端和 JSON.stringify(..) 上收到的结果数组:
[{"Id":1,"Name":"Machine Gunner","Quantity":0},{"Id":2,"Name":"Rocket Soldier","Quantity":0},{"Id":3,"Name":"Paratrooper","Quantity":0},{"Id":4,"Name":"Demolition Soldier","Quantity":0}]
如您所见,结果按预期显示每行的列。