$row
是设计时间,而不是填充值。此值仅在运行时设置,带有数据库查询。
如果您绝对需要,只有一种解决方法mydata->
。
该功能的优点是setProps()
可以验证值。
[...]
define(val1, "name");
define(val2, "address");
$query = "SELECT ".val1.", ".val2." from people where postcode like 'SO%'";
$result = mysql_query($query);
if (!$result) {
$message = 'invalid query: ' . mysql_error() . "\n";
$message .= 'query: ' . $query;
die($message);
}
while($rows[]=mysql_fetch_array($result,MYSQL_ASSOC));
class MyArrayObject extends ArrayObject {
public $name;
public $address;
public function setProps($name) {
if (isset($name[val1])) {$this->name = $name[val1]; }
else {$this->name = 'n/a';}
if (isset($name[val2])) {$this->address = $name[val2]; }
else {$this->address = 'n/a';}
}
}
$mao = new MyArrayObject($rows,ArrayObject::ARRAY_AS_PROPS);
$iterator = $mao->getIterator();
while ($iterator->valid()) {
$mao->setProps($iterator->current());
echo "<br>\n";
echo $mao->name . "<br>\n" ;
echo $mao->address ;
$iterator->next();
}
mysql_close($link);
[...]
代码完成