请帮助PHP代码
$data = mysql_query("SELECT * FROM a2h_member_type") or die(mysql_error());
foreach($data as $row){
foreach($row as $key=>$value){
echo $key;
echo $value;
}
答案 0 :(得分:3)
查看mysql_query的手册页 - 它返回资源句柄,而不是结果数组。您可以使用带有mysql_fetch_assoc()等函数的资源句柄来获取结果集的每一行。
$rs= mysql_query("SELECT * FROM a2h_member_type") or die(mysql_error());
while ($row = mysql_fetch_assoc($rs)) {
//process $row
}
mysql_free_result($rs);
答案 1 :(得分:1)
你应该做一个mysql fetch数组来获取所有结果。
$data = mysql_query("SELECT * FROM a2h_member_type") or die(mysql_error());
while ($row = mysql_fetch_array($data)) {
foreach($row as $key=>$value){
echo $key;
echo $value;
}
}
答案 2 :(得分:0)
你去吧
$result = mysql_query("SELECT * FROM a2h_member_type") or die(mysql_error());
while($data = mysql_fetch_array($result, MYSQL_ASSOC)){
foreach($data as $key => $value){
echo $key;
echo $value;
}
}
干杯
答案 3 :(得分:0)
如果要在mysql_query结果上使用foreach,则必须实现迭代器。例如:
class MySQLResultIterator implements Iterator
{
private $result;
private $current;
private $pos = 0;
public function __construct($result)
{
$this->result = $result;
$this->updateCurrent();
}
public function rewind() {
mysql_data_seek($this->result, 0);
$this->pos = 0;
$this->updateCurrent();
}
public function current() {
return $this->current;
}
public function key() {
return $this->pos;
}
public function next() {
$this->updateCurrent();
$this->pos++;
return $this->current;
}
public function valid() {
return ($this->current != false);
}
private function updateCurrent() {
$this->current = mysql_fetch_assoc($this->result);
}
}
$data = mysql_query("SELECT * FROM a2h_member_type") or die(mysql_error());
$iterator = new MySQLResultIterator($data);
foreach( $iterator as $row )
{
// ...
}